Block/Hide Words

Block/hide specified words from appearing on webpages

  1. // ==UserScript==
  2. // @name Block/Hide Words
  3. // @namespace https://github.com/AbdurazaaqMohammed
  4. // @version 1.0
  5. // @author Abdurazaaq Mohammed
  6. // @description Block/hide specified words from appearing on webpages
  7. // @match https://*.*/*
  8. // @match http://*.*/*
  9. // @homepage https://github.com/AbdurazaaqMohammed/userscripts
  10. // @license The Unlicense
  11. // @supportURL https://github.com/AbdurazaaqMohammed/userscripts/issues
  12. // @grant none
  13. // @run-at document-start
  14. // ==/UserScript==
  15. (function() {
  16. 'use strict';
  17.  
  18. // List of words to be blocked
  19. const blockedWords = ["word1", "word2", "word3"]; // You can add as many words as you want. Just ensure they are inside quotes ("") and separateed by commas (,).
  20.  
  21. function replaceBlockedWords(node) {
  22. if (node.nodeType === Node.TEXT_NODE) {
  23. let text = node.nodeValue;
  24. blockedWords.forEach(word => { text = text.replaceAll(word, ''); });
  25. // You can type anything you want the words to be replaced with inside the quote.
  26. // If you want to replace different sets of words with different words just create another list (copy and paste the const blockedWords line) and copy and paste the blockedWords.forEach line below the existing one in this function replacing both blockedWords with whatever name you want.
  27. node.nodeValue = text;
  28. } else {
  29. node.childNodes.forEach(replaceBlockedWords);
  30. }
  31. }
  32.  
  33.  
  34. const observer = new MutationObserver(mutations => {
  35. mutations.forEach(mutation => {
  36. mutation.addedNodes.forEach(node => replaceBlockedWords(node));
  37. });
  38. });
  39.  
  40. const intervalId = setInterval(function() { // make sure it loads, when you do document-start sometimes it doesn't load
  41. if(document.body.childNodes) {
  42. document.body.childNodes.forEach(replaceBlockedWords);
  43. observer.observe(document.body, { childList: true, subtree: true });
  44. clearInterval(intervalId);
  45. }
  46. }, 100);
  47. })();