cd /news/developer-tools/block-ad-accounts-on-x-twitter-users… · home topics developer-tools article
[ARTICLE · art-8708] src=gist.github.com ↗ pub= topic=developer-tools verified=true sentiment=· neutral

Block Ad accounts on X (Twitter) | UserScript | Tampermonkey

This is a Tampermonkey userscript designed to automatically block advertising accounts on X (formerly Twitter). The script identifies promoted tweets labeled "Ad" on the platform, then programmatically clicks through the interface to block each advertiser account, storing the blocked accounts in the browser's local storage. It runs immediately upon page load and continues to check for new ads every five seconds.

read1 min views18 publishedNov 30, 2024
block-ad-accounts-on-x.userscript.js

      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.

Learn more about bidirectional Unicode characters

Show hidden characters
// ==UserScript==

// @name         Block Ad accounts on X (Twitter)

// @namespace    http://tampermonkey.net/

// @version      2024-11-30

// @description  try to take over the world!

// @author       You

// @match        https://x.com/*

// @icon         https://www.google.com/s2/favicons?sz=64&domain=x.com

// @grant        none

// ==/UserScript==

async function blockAdvertisers() {

  const ads = Array.from(

    document.querySelectorAll('[data-testid="tweet"] [style*="rgb"]')

  ).filter((ad) => /^Ad$/.test(ad.innerText));

  for (const ad of ads) {

    const tweet = ad.closest('[data-testid="tweet"]');

    localStorage.__blockAdvertisers = localStorage.__blockAdvertisers || "{}";

    const btn =

      ad.parentElement.parentElement.parentElement.querySelector("button");

    btn.click();

    await new Promise((r) => setTimeout(r, 100));

    const menu = document.querySelector('[role="menu"]');

    const blockBtn = Array.from(

      menu.querySelectorAll('div[role="menuitem"]')

    ).find((d) => /Block @/.test(d.innerText));

    blockBtn.click();

    await new Promise((r) => setTimeout(r, 100));

    const okBtn = document.querySelector('[role="group"] button');

    okBtn.click();

    await new Promise((r) => setTimeout(r, 1000));

    localStorage.__blockAdvertisers = JSON.stringify({

      ...JSON.parse(localStorage.__blockAdvertisers),

      [tweet.querySelector("a").href.slice(1)]:

        true,

    });

    const maybeLater = Array.from(document.querySelectorAll("button")).find(

      (x) => /Maybe later/.test(x.innerText)

    );

    maybeLater?.click();

    await new Promise((r) => setTimeout(r, 100));

  }

}

blockAdvertisers();

setInterval(() => {

  blockAdvertisers();

}, 5000);
── more in #developer-tools 4 stories · sorted by recency
── more on @x 3 stories trending now
sponsored brought to you by zahid.host 4,200+ EU-deployed projects
reading about agents? ship yours in a single git push.

Run your AI side-project on zahid.host

EU-based hosting, git-push deploys, automatic HTTPS, no cold starts. Free tier with a custom domain — perfect for shipping the agent you just read about.

$git push zahid main
Live at https://your-agent.zahid.host
Get free account → Pricing
from €0/mo · no card required
LIVE [news/block-ad-accounts-on…] indexed:0 read:1min 2024-11-30 ·