Word and Image Blocker

Block words, phrases and images associated with those phrases. For the UI to add and delete words and phrases to block, please also install the following, https://greasyfork.org/en/scripts/481834-word-and-image-blocker-settings-ui

  1. // ==UserScript==
  2. // @name Word and Image Blocker
  3. // @namespace https://github.com/def-initive/
  4. // @version 1.1
  5. // @description Block words, phrases and images associated with those phrases. For the UI to add and delete words and phrases to block, please also install the following, https://greasyfork.org/en/scripts/481834-word-and-image-blocker-settings-ui
  6. // @author def-initive
  7. // @match *://*/*
  8. // @exclude *://github.com/*
  9. // @exclude *://greasyfork.org/*
  10. // @grant none
  11. // @license MIT
  12. // ==/UserScript==
  13.  
  14. (function () {
  15. 'use strict';
  16.  
  17. // Function to hide images in element, its parents, and siblings
  18. function hideImagesInElementAndSiblings(element) {
  19. // Hide images in the element itself
  20. var imagesInElement = element.querySelectorAll('img');
  21. imagesInElement.forEach(function (image) {
  22. image.style.display = "none";
  23. });
  24.  
  25. // Hide images in siblings
  26. var siblings = Array.from(element.parentElement.children);
  27. siblings.forEach(function (sibling) {
  28. if (sibling !== element) {
  29. var imagesInSibling = sibling.querySelectorAll('img');
  30. imagesInSibling.forEach(function (image) {
  31. image.style.display = "none";
  32. });
  33. }
  34. });
  35. }
  36.  
  37. // Function to hide direct parent containers and their siblings' images
  38. // containing occurrences of a phrase on a webpage
  39. function hideParentAndSiblings(phrase) {
  40. var textNodes = document.createTreeWalker(
  41. document.body,
  42. NodeFilter.SHOW_TEXT,
  43. null,
  44. false
  45. );
  46.  
  47. var elementsToRemove = [];
  48.  
  49. while (textNodes.nextNode()) {
  50. var textNode = textNodes.currentNode;
  51. var match = textNode.nodeValue.match(new RegExp(phrase, 'gi'));
  52.  
  53. if (match) {
  54. var parentContainer = textNode.parentElement;
  55. elementsToRemove.push(parentContainer);
  56. }
  57. }
  58.  
  59. elementsToRemove.forEach(function (element) {
  60. // Check if the element is part of the UI created by "Blocked Phrases Settings"
  61. if (!element.closest('.blocked-phrases-settings-container')) {
  62. // Hide parent containers
  63. element.style.color = "black";
  64. element.style.backgroundColor = "black";
  65.  
  66. // Hide images in siblings
  67. hideImagesInElementAndSiblings(element);
  68. }
  69. });
  70. }
  71.  
  72. // Load target phrases from local storage
  73. var storedPhrases = localStorage.getItem('blockedPhrases');
  74. var targetPhrases = storedPhrases ? JSON.parse(storedPhrases) : [];
  75.  
  76. // Hide direct parent containers and images in their siblings
  77. targetPhrases.forEach(function (phrase) {
  78. hideParentAndSiblings(phrase);
  79. });
  80.  
  81. // Observe changes in the DOM to handle dynamically loaded content
  82. var observer = new MutationObserver(function (mutations) {
  83. mutations.forEach(function (mutation) {
  84. if (mutation.addedNodes && mutation.addedNodes.length > 0) {
  85. targetPhrases.forEach(function (phrase) {
  86. hideParentAndSiblings(phrase);
  87. });
  88. }
  89. });
  90. });
  91.  
  92. // Configuration of the observer
  93. var observerConfig = {
  94. childList: true,
  95. subtree: true
  96. };
  97.  
  98. // Start observing the DOM
  99. observer.observe(document.body, observerConfig);
  100.  
  101. })();