Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name Twitter Community Blocker
- // @namespace http://tampermonkey.net/
- // @version 0.1.0
- // @description Hide tweets from specific communities on the timeline.
- // @author Google Gemini
- // @icon https://www.google.com/s2/favicons?domain=twitter.com
- // @downloadURL https://pastebin.com/raw/kx2UUSxs
- // @updateURL https://pastebin.com/raw/kx2UUSxs
- // @match *://twitter.com/*
- // @match *://x.com/*
- // @grant GM_getValue
- // @grant GM_setValue
- // @run-at document-idle
- // ==/UserScript==
- (function() {
- 'use strict';
- // make changes here
- const GM_STORAGE_KEY_BLOCKLIST = 'communitiesToBlock';
- let namesToBlock;
- const initialConfigurableNames = [
- // 'ExampleCommunity1',
- // 'ExampleCommunity2',
- ];
- const storedValue = GM_getValue(GM_STORAGE_KEY_BLOCKLIST);
- if (Array.isArray(storedValue)) {
- namesToBlock = storedValue;
- } else {
- namesToBlock = initialConfigurableNames;
- GM_setValue(GM_STORAGE_KEY_BLOCKLIST, namesToBlock);
- }
- const blockedCommunityNames = namesToBlock.map(name => String(name).trim().toLowerCase()).filter(name => name.length > 0);
- if (blockedCommunityNames.length === 0) {
- // console.log('[CommunityBlocker] No communities are currently configured for blocking.');
- return;
- }
- // stop making changes
- let debounceTimer;
- const hideTweets = () => {
- // console.log('[CommunityBlocker] Checking for tweets to hide...');
- document.querySelectorAll('article:not([data-community-blocker-processed])').forEach(article => {
- article.setAttribute('data-community-blocker-processed', 'true');
- if (article.style.display === 'none') {
- return;
- }
- const communityLinks = article.querySelectorAll('a[href*="/i/communities/"], a[href*="/community/"]');
- for (const link of communityLinks) {
- const linkText = link.textContent?.trim().toLowerCase();
- if (linkText && blockedCommunityNames.includes(linkText)) {
- console.log(`[CommunityBlocker] Hiding tweet from community: ${link.textContent?.trim()}`);
- article.style.display = 'none';
- break;
- }
- }
- });
- };
- const debouncedHideTweets = () => {
- clearTimeout(debounceTimer);
- debounceTimer = setTimeout(hideTweets, 250);
- };
- const observer = new MutationObserver(mutationsList => {
- let newTweetsAdded = false;
- for (const mutation of mutationsList) {
- if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
- for (const node of mutation.addedNodes) {
- if (node.nodeType === Node.ELEMENT_NODE) {
- if (node.tagName === 'ARTICLE' || node.querySelector('article')) {
- newTweetsAdded = true;
- break;
- }
- }
- }
- }
- if (newTweetsAdded) break;
- }
- if (newTweetsAdded) {
- // console.log('[CommunityBlocker] New tweets detected, running debouncedHideTweets.');
- debouncedHideTweets();
- }
- });
- observer.observe(document.body, {
- childList: true,
- subtree: true,
- // attributes: true,
- });
- // setTimeout(debouncedHideTweets, 500);
- window.onload = () => setTimeout(debouncedHideTweets, 100);
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement