HideAnnoyingPopupsLib

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

当前为 2025-05-10 提交的版本,查看 最新版本

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

  1. // ==UserScript==
  2. // @name HideAnnoyingPopupsLib
  3. // @description This script hides the annoying popups that are shown in a web page.
  4. // @grant none
  5. // @run-at document-start
  6. // @version 1.0.1
  7. // @author Cyrano68
  8. // @license MIT
  9. // @namespace https://gf.qytechs.cn/users/788550
  10. // ==/UserScript==
  11.  
  12. (function()
  13. {
  14. "use strict";
  15.  
  16. //----------------------------------------------------------------------------------------------------
  17.  
  18. const myVersion = "1.0.1"; // It must be the same value indicated in @version.
  19. consoleLog(`==> HideAnnoyingPopupsLib: HELLO! Loading script (version: ${myVersion})...`);
  20.  
  21. let mutatedNodesConfig;
  22. let mutatedAttributesConfig;
  23.  
  24. function getZeroFilledMillisecs(dateNow)
  25. {
  26. const millisecs = dateNow.getMilliseconds();
  27. return ("00" + millisecs).slice(-3);
  28. }
  29.  
  30. function consoleLog(text)
  31. {
  32. const dateNow = new Date();
  33. //const now = dateNow.toISOString();
  34. const now = dateNow.toLocaleString() + "." + getZeroFilledMillisecs(dateNow);
  35. console.log(`${now} ${text}`);
  36. }
  37.  
  38. function searchVisibleNode(node, selector)
  39. {
  40. const parentElement = node.parentElement;
  41. return (parentElement === null ? null : parentElement.querySelector(`${selector}:not([style*=\"display:none\"]):not([style*=\"display: none\"])`));
  42. }
  43.  
  44. function onMutationList(mutationList, observer)
  45. {
  46. //mutatedNodesConfig = window.mutatedNodesConfig;
  47. //mutatedAttributesConfig = window.mutatedAttributesConfig;
  48.  
  49. //consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - mutationList.length=${mutationList.length}`);
  50. mutationList.forEach((mutation, i) =>
  51. {
  52. //consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - mutation[${i}] - mutation.type=${mutation.type}`);
  53. if (mutation.type === "childList")
  54. {
  55. const addedNodes = mutation.addedNodes;
  56. if (addedNodes.length > 0)
  57. {
  58. //consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - mutation[${i}] - addedNodes.length=${addedNodes.length}`);
  59. addedNodes.forEach((addedNode, j) =>
  60. {
  61. const selectors = mutatedNodesConfig.selectors;
  62. const onMutatedNode = mutatedNodesConfig.onMutatedNode;
  63.  
  64. for (let i = 0; i < selectors.length; ++i)
  65. {
  66. const selector = selectors[i];
  67.  
  68. const foundNode = searchVisibleNode(addedNode, selector);
  69. if (foundNode !== null)
  70. {
  71. let stopExecution = false;
  72. if (onMutatedNode && typeof(onMutatedNode) === "function")
  73. {
  74. stopExecution = onMutatedNode(foundNode);
  75. }
  76.  
  77. if (!stopExecution)
  78. {
  79. const parentElement = foundNode.parentElement;
  80. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - selector='${selector}' - parentElement: tagName='${parentElement.tagName}', id='${parentElement.id}'`);
  81.  
  82. foundNode.style.display = "none"; // Hide node.
  83. foundNode.remove(); // Remove node. IMPORTANT: Without this instruction the script does NOT work properly.
  84. document.body.style.overflowY = "scroll"; // Show vertical scrollbar.
  85. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - selector='${selector}' - foundNode: tagName='${foundNode.tagName}', classList='${foundNode.classList}' ---> HIDDEN/REMOVED`);
  86. }
  87. }
  88. }
  89. });
  90. }
  91. }
  92. else if (mutation.type === "attributes")
  93. {
  94. const attributeInfos = mutatedAttributesConfig.attributeInfos;
  95. const onMutatedAttribute = mutatedAttributesConfig.onMutatedAttribute;
  96.  
  97. for (let i = 0; i < attributeInfos.length; ++i)
  98. {
  99. let attributeInfo = attributeInfos[i];
  100. let attributeName = attributeInfo.attributeName;
  101. let targetTagName = attributeInfo.targetTagName;
  102.  
  103. if ((mutation.attributeName === attributeName) && (mutation.target.tagName === targetTagName))
  104. {
  105. let stopExecution = false;
  106. if (onMutatedAttribute && typeof(onMutatedAttribute) === "function")
  107. {
  108. stopExecution = onMutatedAttribute(mutation);
  109. }
  110.  
  111. if (!stopExecution)
  112. {
  113. if ((mutation.attributeName === "class") && (mutation.target.tagName === "HTML") && mutation.target.classList.contains("has--adblock"))
  114. {
  115. let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
  116. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
  117. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
  118. mutation.target.classList.remove("has--adblock");
  119. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.classList='${mutation.target.classList}'`);
  120. }
  121. else if ((mutation.attributeName === "class") && (mutation.target.tagName === "BODY") && mutation.target.classList.contains("noScroll"))
  122. {
  123. let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
  124. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
  125. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.classList='${mutation.target.classList}'`);
  126. mutation.target.classList.remove("noScroll");
  127. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.classList='${mutation.target.classList}'`);
  128. }
  129. else if ((mutation.attributeName === "style") && (mutation.target.tagName === "HTML") && (mutation.target.style.overflow === "hidden"))
  130. {
  131. let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
  132. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
  133. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
  134. mutation.target.style.overflow = "";
  135. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
  136. }
  137. else if ((mutation.attributeName === "style") && (mutation.target.tagName === "BODY") && (mutation.target.style.overflow === "hidden"))
  138. {
  139. let newAttributeValue = mutation.target.getAttribute(mutation.attributeName);
  140. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - newAttributeValue='${newAttributeValue}'`);
  141. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - BEFORE: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
  142. mutation.target.style.overflow = "";
  143. consoleLog(`==> HideAnnoyingPopupsLib: onMutationList - AFTER: mutation.target.style.overflow='${mutation.target.style.overflow}'`);
  144. }
  145. }
  146. }
  147. }
  148. }
  149. });
  150. }
  151.  
  152. //----------------------------------------------------------------------------------------------------
  153.  
  154. function configure(inMutationObserverConfig, mutatedNodesConfigIn, mutatedAttributesConfigIn)
  155. {
  156. consoleLog(`==> HideAnnoyingPopupsLib: configure - BEGIN`);
  157.  
  158. // SEE: https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
  159. const MutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver;
  160.  
  161. // Create an observer instance linked to the callback function.
  162. const mutationObserver = new MutationObserver(onMutationList);
  163.  
  164. mutatedNodesConfig = mutatedNodesConfigIn;
  165. mutatedAttributesConfig = mutatedAttributesConfigIn;
  166.  
  167. // Start observing the target node for configured mutations.
  168. mutationObserver.observe(document, inMutationObserverConfig);
  169.  
  170. consoleLog(`==> HideAnnoyingPopupsLib: configure - END`);
  171. }
  172.  
  173. function getVersion()
  174. {
  175. return myVersion;
  176. }
  177.  
  178. // Expose the public interface by returning an object
  179. window.HideAnnoyingPopupsLib =
  180. {
  181. configure: configure,
  182. getVersion: getVersion
  183. };
  184.  
  185. consoleLog("==> HideAnnoyingPopupsLib: Script loaded");
  186. })();

QingJ © 2025

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