Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: MutationObserver can't listen ComputedStyle change event #3

Merged
merged 1 commit into from
Apr 8, 2022

Conversation

Naccl
Copy link
Contributor

@Naccl Naccl commented Apr 8, 2022

刷剧发现个小 bug...

计算属性没有 event 给 MutationObserver 监听,还是得用 setInterval 这种 heavy 一些的办法

@ChenZihan-sudo
Copy link
Owner

好的,我先合并,稍后测试后会更新greasyforks

@ChenZihan-sudo ChenZihan-sudo merged commit 714c28b into ChenZihan-sudo:main Apr 8, 2022
@ChenZihan-sudo
Copy link
Owner

ChenZihan-sudo commented Apr 12, 2022

@Naccl 由于所做的屏蔽管理器的特性是在启动时多次重复执行方法,而在这种广告中嵌入了iframe框架需要加载后等待才可执行,经过多次测试,使用MutationObserver方法并不适用于此,原因是即使只调用一次,在我添加一些优化方法后依然不可控(大概是MutationObserver这个方法我没用过搞不懂它的异步了,弄了半天,确实是有点麻烦 。但是如果独立使用功能是没有问题的。

image

所以我按照你的方法,使用最基础的setinterval重构了一下,如下
同时稍后会更新并注明你的贡献

    function addBlockText() {
        //本屏蔽器自带嘲讽功能[doge]
        if (document.getElementById("ad-text:a")) { document.getElementById("ad-text:a").innerHTML = "Trying Blocking AD..."; }
        if (document.getElementById("ad-text:b")) { document.getElementById("ad-text:b").innerHTML = "[Youtube AD block]"; }
        let blockText = document.getElementsByClassName("ytp-ad-button-text");
        if (blockText) {
            for (let i = 0; i < blockText.length; i++) {
                blockText[i].innerHTML = "Blocking..."
            }
        }
    }

    var isCallSkipShitVideoAD = false;

    function skipShitVideoAD() {
        if (document.getElementsByClassName("ytp-ad-button-icon") && !isCallSkipShitVideoAD && !document.getElementsByClassName('ytp-ad-skip-button').length) {
            console.log("进入执行");

            isCallSkipShitVideoAD = true; //set identifier as true to avoid call this function again

            console.log(new DocumentTimeline().currentTime);

            //Open the shit ad card
            document.getElementsByClassName("ytp-ad-button-icon")[0].click();

            // Wait the card load
            let shitIframeTimes = 0;
            let iframeDomPosi = 0; //find the iframe document posi 
            let shitIframeTimer = setInterval(function() {
                shitIframeTimes++;

                setDisplay("none");

                if (document.getElementById("iframe") || shitIframeTimes > 50) {
                    console.log("[YT] Detected the ad penel");
                    //find the iframe document posi 
                    for (let i = 0; i < window.frames.length; i++) {
                        let frameId = "iframe";
                        try {
                            if (window.frames[i].frameElement.id == frameId) {
                                iframeDomPosi = i;
                                break;
                            }
                        } catch (e) { /*console.log(e);*/ }
                    }
                    clearInterval(shitIframeTimer);
                }
            }, 40);

            //Wait for document loaded
            let waitCardTimes = 0;
            let frameDom;
            let waitCardTimer = setInterval(() => {
                waitCardTimes++;

                let bodyNodesLength = 0; //iframe body length of child node
                try {
                    bodyNodesLength = window.frames[iframeDomPosi].document.body.childNodes.length;
                } catch {}

                if (waitCardTimes > 400 || bodyNodesLength != 0) {
                    //get the iframe document
                    frameDom = window.frames[iframeDomPosi].document;

                    try {
                        let shitStopDisplayBtn = frameDom.childNodes[1].childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[1].childNodes[1].childNodes[0].childNodes[0].childNodes[0].childNodes[0].childNodes[1].childNodes[1].childNodes[0].childNodes[0];
                        shitStopDisplayBtn.click();

                        let deleteClickTimes = 0;
                        let deleteClickTimer = setInterval(() => {
                            deleteClickTimes++;
                            let isCatchErr = false;
                            try {
                                let clickStopAdBtn = frameDom.childNodes[1].childNodes[1].childNodes[9].childNodes[0].childNodes[1].childNodes[1].childNodes[0].childNodes[0].childNodes[1].childNodes[1].childNodes[0];
                                // clickStopAdBtn.click();
                                console.log("获取到", clickStopAdBtn);
                                console.log("===>", new DocumentTimeline().currentTime);
                            } catch (e) {
                                isCatchErr = true;
                                console.log("错误捕获");
                            }

                            if (deleteClickTimes > 40 || isCatchErr == false) {
                                clearInterval(deleteClickTimer);
                            }

                        }, 50);

                        let closeShitPanelBtn = frameDom.childNodes[1].childNodes[1].childNodes[2].childNodes[0].childNodes[0].childNodes[0].childNodes[2].childNodes[0].childNodes[0];
                        closeShitPanelBtn.click();
                    } catch (e) { console.log("2", e); }

                    setTimeout(() => {
                        document.getElementsByTagName("tp-yt-iron-overlay-backdrop")[0].style.display = "";
                        console.log("reback display");
                    }, 3000);

                    clearInterval(waitCardTimer);
                }
            }, 50);

            function setDisplay(str) {
                //* Set the shit backdrop display "none" or ""
                try { document.getElementsByTagName("tp-yt-iron-overlay-backdrop")[0].style.display = str; } catch {}

                //* Set the shit ad card display "none" or ""
                try { document.getElementById("iframe").parentNode.parentNode.style.display = str; } catch {}
            }
        }
    }

@Naccl
Copy link
Contributor Author

Naccl commented Apr 12, 2022

❤️ 好! ❤️

主要是考虑到性能,这种带有可监听事件的不用 MutationObserver 有点可惜了。

按理说,脚本的 AD 检测主循环也可以替换成 MutationObserver,事件驱动总是比轮询高效些,只是重构起来可能麻烦些,不过没关系,能跑的代码就是好代码!❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants