Filter reddit post with certain keywords in the title

Hide elements with specific text content

  1. // ==UserScript==
  2. // @name Filter reddit post with certain keywords in the title
  3. // @namespace Filter reddit post
  4. // @version 1.0.1
  5. // @description Hide elements with specific text content
  6. // @author ein
  7. // @match https://new.reddit.com/*
  8. // @license MIT
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14.  
  15. function getKeywords() {
  16. // edit your keywords here
  17. return /keyword1|keyword2|keyword3/i;
  18. }
  19.  
  20. function hideElements() {
  21. var elements = document.querySelectorAll('._2FCtq-QzlfuN-SwVMUZMM3');
  22. var keywords = getKeywords();
  23. elements.forEach(function(el) {
  24. if (keywords.test(el.innerText)) {
  25. var ancestor = el.closest('._1Qs6zz6oqdrQbR7yE_ntfY, ._3xuFbFM3vrCqdGuKGhhhn0').parentElement.parentElement;
  26. if (ancestor) {
  27. ancestor.style.display = 'none';
  28. }
  29. }
  30. });
  31. }
  32.  
  33. hideElements();
  34. var observer = new MutationObserver(function(mutations) {
  35. mutations.forEach(function(mutation) {
  36. if (mutation.type === 'childList') {
  37. hideElements();
  38. }
  39. });
  40. });
  41. var config = { childList: true, subtree: true };
  42. observer.observe(document.body, config);
  43. })();