HideAnnoyingPopupsLib

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

Ce script ne devrait pas être installé directement. C'est une librairie créée pour d'autres scripts. Elle doit être inclus avec la commande // @require https://update.greasyfork.org/scripts/535551/1651487/HideAnnoyingPopupsLib.js

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

(function()
{
    "use strict";

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

    const myVersion = "1.0.6";  // 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 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)
                {
                    addedNodes.forEach((addedNode, j) =>
                    {
                        if ((mutatedNodesConfig !== undefined) && (mutatedNodesConfig !== null))
                        {
                            const selectors     = mutatedNodesConfig.selectors;
                            const onMutatedNode = mutatedNodesConfig.onMutatedNode;

                            if ((selectors !== undefined) && (selectors !== null))
                            {
                                for (let i = 0; i < selectors.length; ++i)
                                {
                                    const selector  = selectors[i];
                                    const foundNode = searchVisibleNode(addedNode, selector);

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

                                        if (!stopMutationProcessing)
                                        {
                                            const parentElement = foundNode.parentElement;
                                            blib.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.
                                            blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - selector='${selector}' - foundNode: tagName='${foundNode.tagName}', classList='${foundNode.classList}' ---> added node HIDDEN/REMOVED`);
                                        }
                                    }
                                }
                            }
                        }
                    });
                }
            }
            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 - mutation.target.tagName='${mutation.target.tagName}', mutation.attributeName='${mutation.attributeName}'`);
                        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 stopMutationProcessing = false;
                                if (onMutatedAttribute && (typeof(onMutatedAttribute) === "function"))
                                {
                                    blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - i=${i} - BEFORE 'onMutatedAttribute(...)'`);
                                    stopMutationProcessing = onMutatedAttribute(mutation);
                                    blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - i=${i} - AFTER 'onMutatedAttribute(...)' - stopMutationProcessing=${stopMutationProcessing}`);
                                }

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

                                    if ((mutation.attributeName === "class") && (mutation.target.tagName === "HTML") && mutation.target.classList.contains("has--adblock"))
                                    {
                                        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
                                        mutation.target.classList.remove("has--adblock");
                                        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - 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: onMutationList - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
                                        mutation.target.classList.remove("noScroll");
                                        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - 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: onMutationList - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
                                        mutation.target.style.overflow = "";
                                        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - 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: onMutationList - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
                                        mutation.target.style.overflow = "";
                                        blib.consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}' ---> attribute modification REMOVED`);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        });
    }

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