Greasy Fork镜像 支持简体中文。

HideAnnoyingPopupsLib

This script hides the annoying popups that are shown in a web page.

目前為 2025-05-10 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/535551/1586435/HideAnnoyingPopupsLib.js

// ==UserScript==
// @name           HideAnnoyingPopupsLib
// @description    This script hides the annoying popups that are shown in a web page.
// @grant          none
// @run-at         document-start
// @version        1.0.1
// @author         Cyrano68
// @license        MIT
// @namespace      https://gf.qytechs.cn/users/788550
// ==/UserScript==

(function()
{
    "use strict";

    //----------------------------------------------------------------------------------------------------

    const myVersion = "1.0.1";  // It must be the same value indicated in @version.
    consoleLog(`==> HideAnnoyingPopupsLib: HELLO! Loading script (version: ${myVersion})...`);

    let mutatedNodesConfig;
    let mutatedAttributesConfig;

    function getZeroFilledMillisecs(dateNow)
    {
        const millisecs = dateNow.getMilliseconds();
        return ("00" + millisecs).slice(-3);
    }

    function consoleLog(text)
    {
        const dateNow = new Date();
        //const now = dateNow.toISOString();
        const now = dateNow.toLocaleString() + "." + getZeroFilledMillisecs(dateNow);
        console.log(`${now} ${text}`);
    }

    function searchVisibleNode(node, selector)
    {
        const parentElement = node.parentElement;
        return (parentElement === null ? null : parentElement.querySelector(`${selector}:not([style*=\"display:none\"]):not([style*=\"display: none\"])`));
    }

    function onMutationList(mutationList, observer)
    {
        //mutatedNodesConfig      = window.mutatedNodesConfig;
        //mutatedAttributesConfig = window.mutatedAttributesConfig;

        //consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - mutationList.length=${mutationList.length}`);
        mutationList.forEach((mutation, i) =>
        {
            //consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - mutation[${i}] - mutation.type=${mutation.type}`);
            if (mutation.type === "childList")
            {
                const addedNodes = mutation.addedNodes;
                if (addedNodes.length > 0)
                {
                    //consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - mutation[${i}] - addedNodes.length=${addedNodes.length}`);
                    addedNodes.forEach((addedNode, j) =>
                    {
                        const selectors     = mutatedNodesConfig.selectors;
                        const onMutatedNode = mutatedNodesConfig.onMutatedNode;

                        for (let i = 0; i < selectors.length; ++i)
                        {
                            const selector = selectors[i];

                            const foundNode = searchVisibleNode(addedNode, selector);
                            if (foundNode !== null)
                            {
                                let stopExecution = false;
                                if (onMutatedNode && typeof(onMutatedNode) === "function")
                                {
                                    stopExecution = onMutatedNode(foundNode);
                                }

                                if (!stopExecution)
                                {
                                    const parentElement = foundNode.parentElement;
                                    consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - selector='${selector}' - parentElement: tagName='${parentElement.tagName}', id='${parentElement.id}'`);

                                    foundNode.style.display = "none";  // Hide node.
                                    foundNode.remove();  // Remove node. IMPORTANT: Without this instruction the script does NOT work properly.
                                    document.body.style.overflowY = "scroll";  // Show vertical scrollbar.
                                    consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - selector='${selector}' - foundNode: tagName='${foundNode.tagName}', classList='${foundNode.classList}' ---> HIDDEN/REMOVED`);
                                }
                            }
                        }
                    });
                }
            }
            else if (mutation.type === "attributes")
            {
                const attributeInfos     = mutatedAttributesConfig.attributeInfos;
                const onMutatedAttribute = mutatedAttributesConfig.onMutatedAttribute;

                for (let i = 0; i < attributeInfos.length; ++i)
                {
                    let attributeInfo = attributeInfos[i];
                    let attributeName = attributeInfo.attributeName;
                    let targetTagName = attributeInfo.targetTagName;

                    if ((mutation.attributeName === attributeName) && (mutation.target.tagName === targetTagName))
                    {
                        let stopExecution = false;
                        if (onMutatedAttribute && typeof(onMutatedAttribute) === "function")
                        {
                            stopExecution = onMutatedAttribute(mutation);
                        }

                        if (!stopExecution)
                        {
                            if ((mutation.attributeName === "class") && (mutation.target.tagName === "HTML") && mutation.target.classList.contains("has--adblock"))
                            {
                                let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
                                mutation.target.classList.remove("has--adblock");
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.classList='${mutation.target.classList}'`);
                            }
                            else if ((mutation.attributeName === "class") && (mutation.target.tagName === "BODY") && mutation.target.classList.contains("noScroll"))
                            {
                                let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
                                mutation.target.classList.remove("noScroll");
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.classList='${mutation.target.classList}'`);
                            }
                            else if ((mutation.attributeName === "style") && (mutation.target.tagName === "HTML") && (mutation.target.style.overflow === "hidden"))
                            {
                                let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
                                mutation.target.style.overflow = "";
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
                            }
                            else if ((mutation.attributeName === "style") && (mutation.target.tagName === "BODY") && (mutation.target.style.overflow === "hidden"))
                            {
                                let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
                                mutation.target.style.overflow = "";
                                consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
                            }
                        }
                    }
                }
            }
        });
    }

    //----------------------------------------------------------------------------------------------------

    function configure(inMutationObserverConfig, mutatedNodesConfigIn, mutatedAttributesConfigIn)
    {
        consoleLog(`==> HideAnnoyingPopupsLib: configure - BEGIN`);

        // SEE: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
        const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;

        // Create an observer instance linked to the callback function.
        const mutationObserver = new MutationObserver(onMutationList);

        mutatedNodesConfig      = mutatedNodesConfigIn;
        mutatedAttributesConfig = mutatedAttributesConfigIn;

        // Start observing the target node for configured mutations.
        mutationObserver.observe(document, inMutationObserverConfig);

        consoleLog(`==> HideAnnoyingPopupsLib: configure - END`);
    }

    function getVersion()
    {
        return myVersion;
    }

    // Expose the public interface by returning an object
    window.HideAnnoyingPopupsLib =
    {
        configure:  configure,
        getVersion: getVersion
    };

    consoleLog("==> HideAnnoyingPopupsLib: Script loaded");
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址