Twitter spam hider

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name     Twitter spam hider
// @description Hides "who to follow" and "trending now" spam on Twitter's web app by scanning the page for it every second.
// @author   Gercomsci
// @namespace  https://greasyfork.org/de/users/771568-gercomsci
// @version  1.0 beta (2022-02-09)
// @include  https://*.twitter.com/*
// @include  https://twitter.com/*
// @include  https://mobile.twitter.com/*
// (redundant URLs to ensure for compatibility)
// @grant    none
// @license  GPL 3.0
// ==/UserScript==

function hideTwitterSpam() {
// Check if element exists, to prevent an error from terminating the loop.
if ( document.querySelectorAll('[aria-label="Who to follow"]')[0] /* Check if exists */
  && document.querySelectorAll('[aria-label="Who to follow"]')[0].parentNode.style.display != 'none' ) /* Check if not already hidden to prevent repeated console output */ {
     console.log ("Hiding \"Who to follow\" spam")
     document.querySelectorAll('[aria-label="Who to follow"]')[0].parentNode.style.display = 'none'; // hide
  }

if ( document.querySelectorAll('[aria-label="Timeline: Trending now"]')[0] /* Check if exists */
  && document.querySelectorAll('[aria-label="Timeline: Trending now"]')[0].parentNode.style.display != 'none' )  /* Check if not already hidden to prevent repeated console output */ {
     console.log ("Hiding \"Trends for you\" spam")
     document.querySelectorAll('[aria-label="Timeline: Trending now"]')[0].parentNode.style.display = 'none'; // hide
  }
}

hideTwitterSpamInterval = setInterval(hideTwitterSpam, 1000); // Searches for spam and hides it every second. 

// 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.