Word Blocker

Block specific words on webpages

当前为 2024-03-30 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Word Blocker
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Block specific words on webpages
  6. // @match *://*/*
  7. // @grant none
  8. // @author Edu Altamirano
  9. // @website https://www.cocoalopez.com/blog
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // List of words to block
  17. var blockedWords = ['boluarte', 'milei', 'salinas', 'Xochitl', 'Xóchitl', 'Israel', 'Netanyahu', 'israel', 'Xóchitl', 'xochitl', 'Milei', 'Boluarte', 'Israeli', 'Macron', 'macron', 'futbol', 'fútbol', 'Argentina', 'argentina'];
  18.  
  19. // List of websites to filter
  20. var websitesToFilter = ['example.com', 'another-example.com'];
  21.  
  22. // List of websites to exclude from filtering
  23. var excludedWebsites = ['excluded-example.com', 'another-excluded-example.com'];
  24.  
  25. // User choice: 'all', 'list', or 'none'
  26. var userChoice = 'all'; // Change this to your preference
  27.  
  28. // Function to check if an element contains a blocked word
  29. function containsBlockedWord(element) {
  30. var text = element.textContent.toLowerCase();
  31. for (var i = 0; i < blockedWords.length; i++) {
  32. if (text.includes(blockedWords[i])) {
  33. return true;
  34. }
  35. }
  36. return false;
  37. }
  38.  
  39. // Function to hide elements containing blocked words
  40. function hideBlockedElements() {
  41. var elements = document.querySelectorAll('p, h1, h2, h3, h4, h5, h6, a, span');
  42. elements.forEach(function(element) {
  43. if (containsBlockedWord(element)) {
  44. // Add a blur effect before hiding the element
  45. element.style.transition = 'all 0.5s';
  46. element.style.filter = 'blur(10px)';
  47. setTimeout(function() {
  48. element.style.display = 'none';
  49. }, 500);
  50.  
  51. // Also hide the closest parent div
  52. var parent = element.parentElement;
  53. while (parent) {
  54. if (parent.tagName.toLowerCase() === 'div') {
  55. (function(parent) {
  56. parent.style.transition = 'all 0.5s';
  57. parent.style.filter = 'blur(10px)';
  58. setTimeout(function() {
  59. parent.style.display = 'none';
  60. }, 500);
  61. })(parent);
  62. break;
  63. }
  64. parent = parent.parentElement;
  65. }
  66.  
  67.  
  68. // Also hide any preceding img elements within the same parent element
  69. var sibling = element.previousElementSibling;
  70. while (sibling) {
  71. if (sibling.tagName.toLowerCase() === 'img') {
  72. (function(sibling) {
  73. sibling.style.transition = 'all 0.5s';
  74. sibling.style.filter = 'blur(10px)';
  75. setTimeout(function() {
  76. sibling.style.display = 'none';
  77. }, 500);
  78. })(sibling);
  79. }
  80. sibling = sibling.previousElementSibling;
  81. }
  82. }
  83. });
  84.  
  85. // Hide img elements with alt text or sibling a element text containing blocked words
  86. var images = document.querySelectorAll('img');
  87. images.forEach(function(img) {
  88. var altText = img.alt.toLowerCase();
  89. for (var i = 0; i < blockedWords.length; i++) {
  90. if (altText.includes(blockedWords[i])) {
  91. img.style.transition = 'all 0.5s';
  92. img.style.filter = 'blur(10px)';
  93. setTimeout(function() {
  94. img.style.display = 'none';
  95. }, 500);
  96. break;
  97. }
  98. }
  99.  
  100. var sibling = img.nextElementSibling;
  101. while (sibling) {
  102. if (sibling.tagName.toLowerCase() === 'a' && containsBlockedWord(sibling)) {
  103. (function(sibling) {
  104. img.style.transition = 'all 0.5s';
  105. img.style.filter = 'blur(10px)';
  106. setTimeout(function() {
  107. img.style.display = 'none';
  108. }, 500);
  109. })(sibling);
  110. break;
  111. }
  112. sibling = sibling.nextElementSibling;
  113. }
  114. });
  115. }
  116.  
  117. // Check if the current website should be filtered
  118. function shouldFilterWebsite() {
  119. var currentWebsite = window.location.hostname;
  120. if (excludedWebsites.includes(currentWebsite)) {
  121. return false;
  122. } else if (userChoice === 'all' || (userChoice === 'list' && websitesToFilter.includes(currentWebsite))) {
  123. return true;
  124. } else {
  125. return false;
  126. }
  127. }
  128.  
  129. // Run the script after the DOM is fully loaded
  130. window.addEventListener('load', function() {
  131. if (shouldFilterWebsite()) {
  132. hideBlockedElements();
  133. }
  134. }, false);
  135. })();