Advertisement
uHioVK

Twitter Community Blocker Script for Tampermonkey

Jun 4th, 2025 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
JavaScript 3.62 KB | Source Code | 0 0
  1. // ==UserScript==
  2. // @name         Twitter Community Blocker
  3. // @namespace    http://tampermonkey.net/
  4. // @version      0.1.0
  5. // @description  Hide tweets from specific communities on the timeline.
  6. // @author       Google Gemini
  7. // @icon         https://www.google.com/s2/favicons?domain=twitter.com
  8. // @downloadURL  https://pastebin.com/raw/kx2UUSxs
  9. // @updateURL    https://pastebin.com/raw/kx2UUSxs
  10. // @match        *://twitter.com/*
  11. // @match        *://x.com/*
  12. // @grant        GM_getValue
  13. // @grant        GM_setValue
  14. // @run-at       document-idle
  15. // ==/UserScript==
  16.  
  17. (function() {
  18.     'use strict';
  19.  
  20.     // make changes here
  21.     const GM_STORAGE_KEY_BLOCKLIST = 'communitiesToBlock';
  22.     let namesToBlock;
  23.  
  24.     const initialConfigurableNames = [
  25.         // 'ExampleCommunity1',
  26.         // 'ExampleCommunity2',
  27.     ];
  28.  
  29.     const storedValue = GM_getValue(GM_STORAGE_KEY_BLOCKLIST);
  30.  
  31.     if (Array.isArray(storedValue)) {
  32.         namesToBlock = storedValue;
  33.     } else {
  34.         namesToBlock = initialConfigurableNames;
  35.         GM_setValue(GM_STORAGE_KEY_BLOCKLIST, namesToBlock);
  36.     }
  37.  
  38.     const blockedCommunityNames = namesToBlock.map(name => String(name).trim().toLowerCase()).filter(name => name.length > 0);
  39.  
  40.     if (blockedCommunityNames.length === 0) {
  41.         // console.log('[CommunityBlocker] No communities are currently configured for blocking.');
  42.         return;
  43.     }
  44.     // stop making changes
  45.  
  46.     let debounceTimer;
  47.  
  48.     const hideTweets = () => {
  49.         // console.log('[CommunityBlocker] Checking for tweets to hide...');
  50.         document.querySelectorAll('article:not([data-community-blocker-processed])').forEach(article => {
  51.             article.setAttribute('data-community-blocker-processed', 'true');
  52.  
  53.             if (article.style.display === 'none') {
  54.                 return;
  55.             }
  56.  
  57.             const communityLinks = article.querySelectorAll('a[href*="/i/communities/"], a[href*="/community/"]');
  58.  
  59.             for (const link of communityLinks) {
  60.                 const linkText = link.textContent?.trim().toLowerCase();
  61.                 if (linkText && blockedCommunityNames.includes(linkText)) {
  62.                     console.log(`[CommunityBlocker] Hiding tweet from community: ${link.textContent?.trim()}`);
  63.                     article.style.display = 'none';
  64.                     break;
  65.                 }
  66.             }
  67.         });
  68.     };
  69.  
  70.     const debouncedHideTweets = () => {
  71.         clearTimeout(debounceTimer);
  72.         debounceTimer = setTimeout(hideTweets, 250);
  73.     };
  74.  
  75.     const observer = new MutationObserver(mutationsList => {
  76.         let newTweetsAdded = false;
  77.         for (const mutation of mutationsList) {
  78.             if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
  79.                 for (const node of mutation.addedNodes) {
  80.                     if (node.nodeType === Node.ELEMENT_NODE) {
  81.                         if (node.tagName === 'ARTICLE' || node.querySelector('article')) {
  82.                             newTweetsAdded = true;
  83.                             break;
  84.                         }
  85.                     }
  86.                 }
  87.             }
  88.             if (newTweetsAdded) break;
  89.         }
  90.  
  91.         if (newTweetsAdded) {
  92.             // console.log('[CommunityBlocker] New tweets detected, running debouncedHideTweets.');
  93.             debouncedHideTweets();
  94.         }
  95.     });
  96.  
  97.     observer.observe(document.body, {
  98.         childList: true,
  99.         subtree: true,
  100.         // attributes: true,
  101.     });
  102.  
  103.     // setTimeout(debouncedHideTweets, 500);
  104.     window.onload = () => setTimeout(debouncedHideTweets, 100);
  105.  
  106. })();
  107.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement