Skip to content

Instantly share code, notes, and snippets.

@8geonirt
Last active October 21, 2021 15:39
Show Gist options
  • Save 8geonirt/061e0a6f37308085b44ecc9313867f78 to your computer and use it in GitHub Desktop.
Save 8geonirt/061e0a6f37308085b44ecc9313867f78 to your computer and use it in GitHub Desktop.
// Add any trending topic in the next array and start using it. It has to be in lowercase
const blackListedKeywords = ['politics', 'entertainment', 'chumel'];
/**
Just copy and paste this code in your browser console and that's it.
NOTE:
- This code can be implemented as an extension, where the blackListedKeywords array can be created from items in the localStorage.
- The extension can have a small UI where users can enter their blacklisted keywords.
- There is a observer method, it helps to detect when Twitter inserts a new children node on the "What's happening section",
so it can easily detect and remove a trending topic based on a keyword without reloading the page.
*/
const removeTrend = trend => trend.parentElement.removeChild(trend);
const isBlackListedKeyword = item => blackListedKeywords.some(keyword => item.innerText.toLowerCase().includes(keyword));
const removeBlackListedKeywords = () => {
const trends = Array.from(document.querySelectorAll('[data-testid="trend"]'));
const trendsToRemove = trends.filter(isBlackListedKeyword);
trendsToRemove.forEach(removeTrend);
};
const nodesAdded = mutation => mutation.addedNodes.length > 0;
const checkForAddedNodes = mutation => nodesAdded(mutation) && removeBlackListedKeywords();
const observer = new MutationObserver(mutations => mutations.forEach(checkForAddedNodes));
const whatsHappeningSection = document.querySelector('[aria-label="Timeline: Trending now"]');
removeBlackListedKeywords();
observer.observe(whatsHappeningSection, { childList: true });
// uncomment the following lines to remove the "What's happening" sidebar
/*
const whatsHappeningSidebar = document.getElementById('accessible-list-0').parentElement;
whatsHappeningSidebar.parentElement.removeChild(whatsHappeningSidebar)
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment