HideAnnoyingPopupsLib

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

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.greasyfork.org/scripts/535551/1709323/HideAnnoyingPopupsLib.js

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name           HideAnnoyingPopupsLib
// @description    This script hides the annoying popups that are shown in a web page.
// @namespace      https://greasyfork.org/users/788550
// @version        1.0.7
// @author         Cyrano68
// @license        MIT
// @require        https://update.greasyfork.org/scripts/547732/1651464/BasicLib.js
// @grant          none
// @run-at         document-start
// ==/UserScript==

// This is a IIFE (Immediately Invoked Function Expression).
(function()
{
    "use strict";

    const blib = window.BasicLib;
    blib.consoleLog(`==> HideAnnoyingPopupsLib: Using library 'BasicLib' (version: ${blib.getVersion()})`);

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

    let showDebugLog = false;
    let mutatedNodesConfig;
    let mutatedAttributesConfig;

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

    function onMutatedNodeDefault(mutation, selector, foundNode)
    {
        const parentElement = foundNode.parentElement;
        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - 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.

        const oldBodyStyle = document.body.getAttribute('style');
        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - OLD bodyStyle=${oldBodyStyle}`);

        if (document.body.style.position === "fixed")
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - Removing style 'position: fixed' from BODY`);
            document.body.style.position = "";
            const newBodyStyle = document.body.getAttribute('style');
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - NEW bodyStyle=${newBodyStyle}`);
        }
        if (document.body.style.overflow === "hidden")
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - Removing style 'overflow: hidden' from BODY`);
            document.body.style.overflow = "";
            const newBodyStyle = document.body.getAttribute('style');
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - NEW bodyStyle=${newBodyStyle}`);
        }

        document.body.style.overflowY = "scroll";  // Show vertical scrollbar.
        const newBodyStyle = document.body.getAttribute('style');
        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - NEW bodyStyle=${newBodyStyle}`);

        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedNodeDefault - selector='${selector}' - foundNode: tagName='${foundNode.tagName}', classList='${foundNode.classList}' ---> The 'addedNode' has been HIDDEN/REMOVED`);
    }

    function onMutatedAttributeDefault(mutation)
    {
        const oldAttributeValue = mutation.oldValue;
        const newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - oldAttributeValue='${oldAttributeValue}', newAttributeValue='${newAttributeValue}'`);

        if ((mutation.attributeName === "class") && (mutation.target.tagName === "HTML") && mutation.target.classList.contains("has--adblock"))
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
            mutation.target.classList.remove("has--adblock");
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - AFTER: mutation.target.classList='${mutation.target.classList}' ---> attribute modification REMOVED`);
        }
        else if ((mutation.attributeName === "class") && (mutation.target.tagName === "BODY") && mutation.target.classList.contains("noScroll"))
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
            mutation.target.classList.remove("noScroll");
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - AFTER: mutation.target.classList='${mutation.target.classList}' ---> attribute modification REMOVED`);
        }
        else if ((mutation.attributeName === "style") && (mutation.target.tagName === "HTML") && (mutation.target.style.overflow === "hidden"))
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
            mutation.target.style.overflow = "";
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}' ---> attribute modification REMOVED`);
        }
        else if ((mutation.attributeName === "style") && (mutation.target.tagName === "BODY") && (mutation.target.style.overflow === "hidden"))
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
            mutation.target.style.overflow = "";
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutatedAttributeDefault - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}' ---> attribute modification REMOVED`);
        }
    }

    function onMutationList(mutationList, observer)
    {
        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- mutationList.length=${mutationList.length}`, showDebugLog);
        mutationList.forEach((mutation, i) =>
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- i=${i} - mutation.type=${mutation.type}`, showDebugLog);
            if (mutation.type === "childList")
            {
                blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- i=${i} - mutation.addedNodes.length=${mutation.addedNodes.length}`, showDebugLog);
                const addedNodes = mutation.addedNodes;
                if (addedNodes.length > 0)
                {
                    blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- addedNodes.length=${addedNodes.length}'`, showDebugLog);
                    addedNodes.forEach((addedNode, j) =>
                    {
                        if ((mutatedNodesConfig !== undefined) && (mutatedNodesConfig !== null))
                        {
                            const selectors     = mutatedNodesConfig.selectors;
                            const onMutatedNode = mutatedNodesConfig.onMutatedNode;

                            if ((selectors !== undefined) && (selectors !== null))
                            {
                                blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- selectors.length=${selectors.length}'`, showDebugLog);
                                for (let k = 0; k < selectors.length; ++k)
                                {
                                    const selector  = selectors[k];
                                    const foundNode = searchVisibleNode(addedNode, selector);

                                    if ((foundNode !== undefined) && (foundNode !== null))
                                    {
                                        let stopMutationProcessing = false;
                                        if (onMutatedNode && (typeof(onMutatedNode) === "function"))
                                        {
                                            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - i=${i}, j=${j}, k=${k} - BEFORE 'onMutatedNode(...)'`);
                                            stopMutationProcessing = onMutatedNode(mutation, selector, foundNode);
                                            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - i=${i}, j=${j}, k=${k} - AFTER 'onMutatedNode(...)' - stopMutationProcessing=${stopMutationProcessing}`);
                                        }

                                        if (stopMutationProcessing)
                                        {
                                            break;
                                        }
                                        else
                                        {
                                            onMutatedNodeDefault(mutation, selector, foundNode);
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            }
            else if (mutation.type === "attributes")
            {
                blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- i=${i} - mutation.target.tagName='${mutation.target.tagName}', mutation.attributeName='${mutation.attributeName}', mutation.oldValue='${mutation.oldValue}'`, showDebugLog);
                if ((mutatedAttributesConfig !== undefined) && (mutatedAttributesConfig !== null))
                {
                    const attributeInfos     = mutatedAttributesConfig.attributeInfos;
                    const onMutatedAttribute = mutatedAttributesConfig.onMutatedAttribute;

                    if ((attributeInfos !== undefined) && (attributeInfos !== null))
                    {
                        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList -D- attributeInfos.length=${attributeInfos.length}'`, showDebugLog);
                        for (let j = 0; j < attributeInfos.length; ++j)
                        {
                            let attributeInfo = attributeInfos[j];
                            let attributeName = attributeInfo.attributeName;
                            let targetTagName = attributeInfo.targetTagName;

                            if ((mutation.attributeName === attributeName) && (mutation.target.tagName === targetTagName))
                            {
                                let stopMutationProcessing = false;
                                if (onMutatedAttribute && (typeof(onMutatedAttribute) === "function"))
                                {
                                    blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - i=${i}, j=${j} - BEFORE 'onMutatedAttribute(...)'`);
                                    stopMutationProcessing = onMutatedAttribute(mutation);
                                    blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - i=${i}, j=${j} - AFTER 'onMutatedAttribute(...)' - stopMutationProcessing=${stopMutationProcessing}`);
                                }

                                if (stopMutationProcessing)
                                {
                                    break;
                                }
                                else
                                {
                                    onMutatedAttributeDefault(mutation);
                                }
                            }
                        }
                    }
                }
            }
        });
    }

    function configure(mutationObserverConfigIn, mutatedNodesConfigIn, mutatedAttributesConfigIn)
    {
        blib.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;

        let selectorsLength      = 0;
        let hasOnMutatedNodeFunc = false;
        if ((mutatedNodesConfig !== undefined) && (mutatedNodesConfig !== null))
        {
            const selectors = mutatedNodesConfig.selectors;
            if ((selectors !== undefined) && (selectors !== null))
            {
                selectorsLength = mutatedNodesConfig.selectors.length;
            }

            const onMutatedNode = mutatedNodesConfig.onMutatedNode;
            if (onMutatedNode && (typeof(onMutatedNode) === "function"))
            {
                hasOnMutatedNodeFunc = true;
            }
        }
        if (selectorsLength == 0)
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: configure - mutatedNodesConfig.selectors.length=${selectorsLength}, hasOnMutatedNodeFunc=${hasOnMutatedNodeFunc}`);
        }
        else
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: configure - mutatedNodesConfig.selectors.length=${selectorsLength}, mutatedNodesConfig.selectors='${mutatedNodesConfig.selectors}', hasOnMutatedNodeFunc=${hasOnMutatedNodeFunc}`);
        }

        let attributeInfosLength      = 0;
        let hasOnMutatedAttributeFunc = false;
        if ((mutatedAttributesConfig !== undefined) && (mutatedAttributesConfig !== null))
        {
            const attributeInfos = mutatedAttributesConfig.attributeInfos;
            if ((attributeInfos !== undefined) && (attributeInfos !== null))
            {
                attributeInfosLength = mutatedAttributesConfig.attributeInfos.length;
            }

            const onMutatedAttribute = mutatedAttributesConfig.onMutatedAttribute;
            if (onMutatedAttribute && (typeof(onMutatedAttribute) === "function"))
            {
                hasOnMutatedAttributeFunc = true;
            }
        }
        if (attributeInfosLength == 0)
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: configure - mutatedAttributesConfig.attributeInfos.length=${attributeInfosLength}, hasOnMutatedAttributeFunc=${hasOnMutatedAttributeFunc}`);
        }
        else
        {
            blib.consoleLog(`==> HideAnnoyingPopupsLib: configure - mutatedAttributesConfig.attributeInfos.length=${attributeInfosLength}, mutatedAttributesConfig.attributeInfos='${JSON.stringify(mutatedAttributesConfig.attributeInfos)}, hasOnMutatedAttributeFunc=${hasOnMutatedAttributeFunc}'`);
        }

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

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

    function getVersion()
    {
        return myVersion;
    }

    function setShowDebugLog(showDebugLogIn)
    {
        showDebugLog = showDebugLogIn;
        blib.consoleLog(`==> HideAnnoyingPopupsLib: setShowDebugLog - showDebugLog=${showDebugLog}`);
    }

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

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