Page Filter

Removes links, images, and text which refer to specific keywords. When a keyword is found in an URL of a link or image, the link/image will be removed. When a keyword is found in a text, the whole text in its container element, will be removed.

  1. // ==UserScript==
  2. // @name Page Filter
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0.1
  5. // @license GNU AGPLv3
  6. // @description Removes links, images, and text which refer to specific keywords. When a keyword is found in an URL of a link or image, the link/image will be removed. When a keyword is found in a text, the whole text in its container element, will be removed.
  7. // @author jcunews
  8. // @match *://*/*
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13.  
  14. //*** CONFIGURATION BEGIN ***
  15.  
  16. //Below regular expression will be compared against URLs and text.
  17. //Put anything there to remove them from the page.
  18. var rx = /\b(removeme|deleteme)\b/gi;
  19.  
  20. //*** CONFIGURATION END ***
  21.  
  22. function processElement(node, url, nextNode, styles) {
  23. if (rx.test(node.href || node.src) || ((styles = getComputedStyle(node)) && rx.test(styles.backgroundImage))) {
  24. if (rx.test(node.parentNode.textContent)) {
  25. node.parentNode.innerHTML = "";
  26. } else node.remove();
  27. } else {
  28. for (node = node.childNodes[0]; node; node = nextNode) {
  29. nextNode = node.nextSibling;
  30. processNode(node);
  31. }
  32. }
  33. }
  34.  
  35. function processNode(node) {
  36. switch (node.nodeType) {
  37. case Node.ELEMENT_NODE:
  38. processElement(node);
  39. break;
  40. case Node.TEXT_NODE:
  41. if (rx.test(node.nodeValue)) node.nodeValue = "";
  42. break;
  43. }
  44. }
  45.  
  46. processNode(document.body);
  47.  
  48. (new MutationObserver(function(records) {
  49. records.forEach(function(record) {
  50. if (record.type === "characterData") {
  51. if (rx.test(record.target.nodeValue)) record.target.nodeValue = "";
  52. } else record.addedNodes.forEach(processNode);
  53. });
  54. })).observe(document.body, {childList: true, characterData: true, subtree: true});
  55. })();