Skip to content

Instantly share code, notes, and snippets.

@webfussel
Last active March 13, 2023 07:58
Show Gist options
  • Save webfussel/ee6299ba87a53beb6aa6a2aaeca256fe to your computer and use it in GitHub Desktop.
Save webfussel/ee6299ba87a53beb6aa6a2aaeca256fe to your computer and use it in GitHub Desktop.
Remove Twittertrends and Sponsored Tweets/Follow Suggestions - Tampermonkey Script
// ==UserScript==
// @name Remove Twittertrends and Sponsored Tweets/Follow Suggestions
// @namespace http://webfussel.de
// @version 1.5.1
// @description Remove those stupid twitter trends and sponsored ads / follow suggestions
// @author webfussel
// @match https://twitter.com/*
// @grant none
// ==/UserScript==
const sponsored = ['Gesponsert', 'Sponsored']
const sponsoredRegexp = new RegExp(sponsored.join('|'))
const removeElement = e => {
// Remove sponsored
// Tweets
if (e.matches('[data-testid="placementTracking"]') && e.innerText.match(sponsoredRegexp)) e.parentElement.parentElement.innerHTML = ''
// Follow Suggestions
if (e.matches('[data-testid="UserCell"]') && e.innerText.match(sponsoredRegexp)) e.remove()
}
const removeTrends = () => {
// Twitter Blue Menu Point
document.querySelector('[aria-label="Twitter Blue"]')?.remove()
// Trends
document.querySelector('[href*="explore"]')?.remove()
const lists = document.querySelectorAll('[aria-labelledby^="accessible-l"]')
lists?.forEach(element => {
if (element.querySelector(':scope > h1')?.innerText.toLowerCase().indexOf('trends') > -1) {
element.parentElement.parentElement.remove()
}
})
}
const observer = new MutationObserver(mutations => {
removeTrends()
mutations.forEach(mutation => {
mutation.addedNodes.forEach(removeElement)
})
})
observer.observe(document.body, {
childList: true
,subtree: true
,attributes: false
,characterData: false
})
@bensh90
Copy link

bensh90 commented Jul 24, 2021

// ==UserScript==
// @name         Remove Twittertrends and Sponsored Tweets/Follow Suggestions
// @namespace    http://webfussel.de
// @version      1.1
// @description  Remove those stupid twitter trends and sponsored ads / follow suggestions
// @author       webfussel
// @match        https://twitter.com/*
// @grant        none
// ==/UserScript==

(() => {
    var observer = new MutationObserver((mutations) => {

        // Remove sponsored
        // Tweets
        const sponsored = ['Gesponsert', 'Sponsored']
        const sponsoredRegexp = new RegExp(sponsored.join('|'))
        ;[...document.querySelectorAll('[data-testid="placementTracking"]')]?.forEach(ad => {
            if (ad.innerText.match(sponsoredRegexp)) ad.parentElement.parentElement.innerHTML = ''
        })
        // Follow Suggetions
        ;[...document.querySelectorAll('[data-testid="UserCell"]')]?.forEach(ad => {
            if (ad.innerText.match(sponsoredRegexp)) ad.remove()
        })

        // Remove trends
        document.querySelector('[href*="explore"]')?.remove()
        const lists = document.querySelectorAll('[aria-labelledby^="accessible-l"]')
        lists?.forEach(element => {
            if (element.querySelector(':scope > h1')?.innerText.toLowerCase().indexOf('trends') >-1) {
                element.parentElement.parentElement.parentElement.remove()
            }
        })
})

    observer.observe(document.body, {
        childList: true
        ,subtree: true
        ,attributes: false
        ,characterData: false
    })

})();

@webfussel
Copy link
Author

Thanks! I implemented it :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment