

群里做跨境电商的兄弟求助之前社区的方法在cdiscount平台不好用,今天分享一下!!



// ==UserScript==
// @name Shadow-root(closed)
// @namespace http://your-domain.com
// @version 1.2
// @description 强制解除shadow-root(closed)限制
// @match *://*/*
// @run-at document-start
// @grant unsafeWindow
// @grant GM_addStyle
// ==/UserScript==
(function() {
'use strict';
// ================== 核心破解逻辑 ==================
const hijackShadowDOM = () => {
// 劫持原生API
const nativeAttachShadow = Element.prototype.attachShadow;
Element.prototype.attachShadow = function(config) {
// 强制修改closed模式为open
if (config?.mode === 'closed') {
console.debug('[破解] 检测到closed模式,已强制开启');
config.mode = 'open';
}
return nativeAttachShadow.call(this, config);
};
// 框架级破解(Vue/React)
if (unsafeWindow.Vue) {
const originalVueMount = unsafeWindow.Vue.prototype.$mount;
unsafeWindow.Vue.prototype.$mount = function(el) {
this.$el = el;
return originalVueMount.call(this, el);
};
console.debug('[破解] Vue组件Shadow DOM限制已解除');
}
};
// ================== 反检测机制 ==================
const antiDetection = () => {
// 屏蔽控制台特征
const originalConsoleLog = console.log;
console.log = function(...args) {
if (!args.some(str => str.includes('shadow'))) {
originalConsoleLog.apply(console, args);
}
};
// 阻断异常检测
const originalAddEventListener = EventTarget.prototype.addEventListener;
EventTarget.prototype.addEventListener = function(type, listener, options) {
if (type === 'securitypolicyviolation') return;
originalAddEventListener.call(this, type, listener, options);
};
};
// ================== DOM监控模块 ==================
const startDOMObserver = () => {
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === 1 && node.shadowRoot) {
node.shadowRoot.innerHTML +=
'<style>:host { display: block !important; }</style>';
}
});
});
});
observer.observe(document.documentElement, {
childList: true,
subtree: true,
attributes: false,
characterData: false
});
};
// ================== 执行入口 ==================
hijackShadowDOM();
antiDetection();
startDOMObserver();
// 注入辅助样式
GM_addStyle(`
#shadow-root (closed) {
display: block !important;
border: 1px dashed red !important;
}
`);
// 调试模式开关(生产环境注释掉)
// unsafeWindow.__DEBUG_SHADOW__ = true;
})();