This script hides the annoying popups that are shown in a web page.
当前为
此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.cn-greasyfork.org/scripts/535551/1586801/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.3
// @author Cyrano68
// @license MIT
// @namespace https://greasyfork.org/users/788550
// ==/UserScript==
(function()
{
"use strict";
const myVersion = "1.0.3"; // 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)
{
//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) =>
{
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 stopExecution = false;
if (onMutatedNode && (typeof(onMutatedNode) === "function"))
{
stopExecution = onMutatedNode(mutation, 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")
{
if ((mutatedAttributesConfig !== undefined) && (mutatedAttributesConfig !== null))
{
const attributeInfos = mutatedAttributesConfig.attributeInfos;
const onMutatedAttribute = mutatedAttributesConfig.onMutatedAttribute;
if ((attributeInfos !== undefined) && (attributeInfos !== null))
{
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;
let arrayLength = 0;
if ((mutatedNodesConfig !== undefined) && (mutatedNodesConfig !== null))
{
const selectors = mutatedNodesConfig.selectors;
if ((selectors !== undefined) && (selectors !== null))
{
arrayLength = mutatedNodesConfig.selectors.length;
}
}
consoleLog(`==> HideAnnoyingPopupsLib: configure - mutatedNodesConfig.selectors.length=${arrayLength}`);
arrayLength = 0;
if ((mutatedAttributesConfig !== undefined) && (mutatedAttributesConfig !== null))
{
const attributeInfos = mutatedAttributesConfig.attributeInfos;
if ((attributeInfos !== undefined) && (attributeInfos !== null))
{
arrayLength = mutatedAttributesConfig.attributeInfos.length;
}
}
consoleLog(`==> HideAnnoyingPopupsLib: configure - mutatedAttributesConfig.attributeInfos.length=${arrayLength}`);
// 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 =
{
consoleLog: consoleLog,
configure: configure,
getVersion: getVersion
};
consoleLog("==> HideAnnoyingPopupsLib: Script loaded");
})();