shadow-root(close)➡shadow-root(open)油猴篡改
评论
收藏

shadow-root(close)➡shadow-root(open)油猴篡改

经验分享
码上电商
2025-06-12 16:27·浏览量:1056
码上电商
影刀中级开发者
发布于 2025-06-12 16:271056浏览
群里做跨境电商的兄弟求助之前社区的方法在cdiscount平台不好用,今天分享一下!!

1.浏览器下载油猴插件(tampermonkey)


2.新增脚本并启动(代码放在最后)


3.成果展示

4.代码

// ==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;
})();


收藏18
全部评论1
最新
发布评论
评论