Robust Popup Blocker with Whitelist

Blocks all popups unless they originate from a whitelisted domain, with enhanced handling.

当前为 2024-04-15 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Robust Popup Blocker with Whitelist
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Blocks all popups unless they originate from a whitelisted domain, with enhanced handling.
  6. // @author Your Name
  7. // @match *://*/*
  8. // @grant none
  9. // @run-at document-start
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // List of trusted websites or domains where redirects are allowed
  17. const whitelist = [
  18. '500px.com',
  19. 'adobe.com',
  20. 'amazon.com',
  21. 'apple.com',
  22. 'archlinux.org',
  23. 'arstechnica.com',
  24. 'artstation.com',
  25. 'asana.com',
  26. 'atlassian.com',
  27. 'axios.com',
  28. 'battle.net',
  29. 'bbc.com',
  30. 'behance.net',
  31. 'bestbuy.com',
  32. 'blogger.com',
  33. 'booking.com',
  34. 'buzzfeed.com',
  35. 'canva.com',
  36. 'cnn.com',
  37. 'codecademy.com',
  38. 'constantcontact.com',
  39. 'coursera.org',
  40. 'deviantart.com',
  41. 'discord.com',
  42. 'docusign.com',
  43. 'dribbble.com',
  44. 'dropbox.com',
  45. 'duolingo.com',
  46. 'ebay.com',
  47. 'edx.org',
  48. 'engadget.com',
  49. 'epicgames.com',
  50. 'etsy.com',
  51. 'eurogamer.net',
  52. 'facebook.com',
  53. 'figma.com',
  54. 'flickr.com',
  55. 'forbes.com',
  56. 'framer.com',
  57. 'freecodecamp.org',
  58. 'gamespot.com',
  59. 'gettyimages.com',
  60. 'github.com',
  61. 'gizmodo.com',
  62. 'gog.com',
  63. 'hubspot.com',
  64. 'huffpost.com',
  65. 'humblebundle.com',
  66. 'ign.com',
  67. 'ikea.com',
  68. 'imdb.com',
  69. 'imgur.com',
  70. 'instagram.com',
  71. 'intuit.com',
  72. 'invisionapp.com',
  73. 'itch.io',
  74. 'khanacademy.org',
  75. 'kotaku.com',
  76. 'lifehacker.com',
  77. 'linkedin.com',
  78. 'lynda.com',
  79. 'mailchimp.com',
  80. 'mashable.com',
  81. 'masterclass.com',
  82. 'medium.com',
  83. 'microsoft.com',
  84. 'mozilla.org',
  85. 'msn.com',
  86. 'netflix.com',
  87. 'nytimes.com',
  88. 'origin.com',
  89. 'paypal.com',
  90. 'pcgamer.com',
  91. 'pexels.com',
  92. 'pinterest.com',
  93. 'pixabay.com',
  94. 'pluralsight.com',
  95. 'polygon.com',
  96. 'quora.com',
  97. 'reddit.com',
  98. 'salesforce.com',
  99. 'samsung.com',
  100. 'shutterstock.com',
  101. 'sketch.com',
  102. 'skillshare.com',
  103. 'skype.com',
  104. 'slack.com',
  105. 'somegit.dev',
  106. 'soundcloud.com',
  107. 'spotify.com',
  108. 'stackoverflow.com',
  109. 'steamcommunity.com',
  110. 'surveymonkey.com',
  111. 'target.com',
  112. 'techcrunch.com',
  113. 'theguardian.com',
  114. 'theverge.com',
  115. 'tiktok.com',
  116. 'trello.com',
  117. 'tripadvisor.com',
  118. 'tumblr.com',
  119. 'twitch.tv',
  120. 'twitter.com',
  121. 'udemy.com',
  122. 'unsplash.com',
  123. 'Vice.com',
  124. 'vimeo.com',
  125. 'vk.com',
  126. 'vox.com',
  127. 'walmart.com',
  128. 'washingtonpost.com',
  129. 'whatsapp.com',
  130. 'wikimedia.org',
  131. 'wikipedia.org',
  132. 'wired.com',
  133. 'wordpress.com',
  134. 'wsj.com',
  135. 'yahoo.com',
  136. 'yelp.com',
  137. 'youtube.com',
  138. 'zapier.com',
  139. 'zendesk.com',
  140. 'zeplin.io',
  141. 'zoom.us',
  142. 'google.com',
  143. 'wiki.archlinux.org'
  144. // Add more trusted websites or domains here
  145. ];
  146.  
  147. function isWhitelisted(url) {
  148. try {
  149. const hostname = new URL(url).hostname;
  150. return whitelist.some(domain => hostname === domain || hostname.endsWith('.' + domain));
  151. } catch (e) {
  152. console.error('Error checking whitelist:', e);
  153. return false;
  154. }
  155. }
  156.  
  157. const originalOpen = window.open;
  158. window.open = function(url, name, features) {
  159. if (!url || isWhitelisted(url)) {
  160. return originalOpen.call(this, url, name, features);
  161. } else {
  162. console.log('Blocked popup from:', url);
  163. return null;
  164. }
  165. };
  166.  
  167. // Disable other known methods to open a window
  168. window.alert = window.confirm = window.prompt = function() {
  169. console.log('Blocked suspicious popup interaction');
  170. return null;
  171. };
  172.  
  173. // Observe and handle inline event triggers
  174. document.addEventListener('DOMContentLoaded', () => {
  175. const allElements = document.querySelectorAll('body *');
  176. allElements.forEach(el => {
  177. const isInlinePopup = el.getAttribute('onclick')?.includes('window.open');
  178. if (isInlinePopup) {
  179. el.removeAttribute('onclick');
  180. console.log('Removed inline popup trigger from element:', el);
  181. }
  182. });
  183. });
  184.  
  185. console.log('Popup blocker initialized.');
  186. })();