Twitter spam hider

Hides "who to follow" and "trending now" spam on Twitter's web app by scanning the page for it every second.

  1. // ==UserScript==
  2. // @name Twitter spam hider
  3. // @description Hides "who to follow" and "trending now" spam on Twitter's web app by scanning the page for it every second.
  4. // @author Gercomsci
  5. // @namespace https://greasyfork.org/de/users/771568-gercomsci
  6. // @version 1.0 beta (2022-02-09)
  7. // @include https://*.twitter.com/*
  8. // @include https://twitter.com/*
  9. // @include https://mobile.twitter.com/*
  10. // (redundant URLs to ensure for compatibility)
  11. // @grant none
  12. // @license GPL 3.0
  13. // ==/UserScript==
  14.  
  15. function hideTwitterSpam() {
  16. // Check if element exists, to prevent an error from terminating the loop.
  17. if ( document.querySelectorAll('[aria-label="Who to follow"]')[0] /* Check if exists */
  18. && document.querySelectorAll('[aria-label="Who to follow"]')[0].parentNode.style.display != 'none' ) /* Check if not already hidden to prevent repeated console output */ {
  19. console.log ("Hiding \"Who to follow\" spam")
  20. document.querySelectorAll('[aria-label="Who to follow"]')[0].parentNode.style.display = 'none'; // hide
  21. }
  22.  
  23. if ( document.querySelectorAll('[aria-label="Timeline: Trending now"]')[0] /* Check if exists */
  24. && document.querySelectorAll('[aria-label="Timeline: Trending now"]')[0].parentNode.style.display != 'none' ) /* Check if not already hidden to prevent repeated console output */ {
  25. console.log ("Hiding \"Trends for you\" spam")
  26. document.querySelectorAll('[aria-label="Timeline: Trending now"]')[0].parentNode.style.display = 'none'; // hide
  27. }
  28. }
  29.  
  30. hideTwitterSpamInterval = setInterval(hideTwitterSpam, 1000); // Searches for spam and hides it every second.
  31.  
  32. // This is the best possible solution on a ReactJS-based site, since it uses randomized CSS class names, meaning the only way to hide spam selectively is to make JavaScript code locate it on the page continuously, whack-a-mole style.