skip to content
Just Change Direction

How to remove ads from your Twitter timeline

/ 1 min read

I have wondered how easy it would be to just remove Ad Tweets from the DOM.

Turns out it is pretty easy. Here is a small script

const removeAds = () => {
// Get all the loaded tweets
const nodes = document.querySelectorAll('[data-testid="cellInnerDiv"]');
for (let item of nodes) {
// Check if it is an Ad tweet
const isSpanAdPresent = Array.from(item.querySelectorAll("span")).some(
(span) => span.innerText.trim() === "Ad",
);
if (isSpanAdPresent) {
// Delete if so
item.remove();
}
}
};

I am not sure what to do with it yet but if I get around to it I will likely add it to a local web extension I have (which does random useful things like redirect Google Maps to use my prefferred Google account). I guess I can run it every X seconds or upon scroll.

Although it seems like the timeline sometimes crashes when using it so perhaps Twitter (understandably) has some logic to protect against users doing this.