Skip to content

Instantly share code, notes, and snippets.

@byronaltice
Forked from gaulinmp/nopromotedtweets.js
Last active July 23, 2021 17:23
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byronaltice/8aeba3f8e1cbd565a569cba5111f9d4a to your computer and use it in GitHub Desktop.
Save byronaltice/8aeba3f8e1cbd565a569cba5111f9d4a to your computer and use it in GitHub Desktop.
Tampermonkey script to remove promoted tweets
// ==UserScript==
// @name Twitter Promoted
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Get rid of stupid promoted tweets
// @author Mac Gaulin
// @match http*://twitter.com/*
// @grant none
// ==/UserScript==
var numberTweetsHidden = 0;
var previousNumberTweetsHidden = 0;
function contains(selector, text) {
let elements = document.querySelectorAll(selector);
return Array.from(elements).filter(function(element) {
return RegExp(text).test(element.textContent);
});
}
function removePromoted() {
let found = contains("span", "^Promoted");
for (let i = 0; i < found.length; i++) {
let elem = found[i];
let article = elem.closest("article");
if (article) {
article.parentNode.style.backgroundColor = "red";
article.parentNode.removeChild(article);
numberTweetsHidden++;
}
}
if(numberTweetsHidden - previousNumberTweetsHidden != 0) {
console.log("found " + (numberTweetsHidden - previousNumberTweetsHidden) + " promoted tweets and hid them all");
previousNumberTweetsHidden = numberTweetsHidden;
}
}
setInterval(removePromoted, 1000);
@byronaltice
Copy link
Author

The original script wouldn't hide the elements until they scrolled into view, but removeChild does

The original also only ran once, so if you scrolled into new elements it wouldn't find those. Now it runs every second.

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