Skip to content

Instantly share code, notes, and snippets.

@trinlol
Forked from adalinesimonian/block-the-blue.md
Created February 21, 2024 22:56
Show Gist options
  • Save trinlol/f9f49013513e7342ebbd03a6e90a94fa to your computer and use it in GitHub Desktop.
Save trinlol/f9f49013513e7342ebbd03a6e90a94fa to your computer and use it in GitHub Desktop.
Block all verified Twitter accounts on screen

#BlockTheBlue

Table of Contents

What is this?

The script at the bottom will block verified Twitter accounts whose tweets are visible on the current page.

Screen capture

To run this script, paste it into your browser's developer tools console, usually accessible by pressing F12 or Cmd+Shift+I.

To stop the execution of this script for any reason, just refresh the page.

Bookmarklet

You can also pin this script to your browser's bookmarks bar, and click it to run it on any Twitter page. To do this, create a new bookmark, and paste the following code into the URL field:

javascript:(async()=>{const e=e=>new Promise(t=>{let o=document.querySelector(e);if(o)return void t(o);const l=new MutationObserver(()=>{(o=document.querySelector(e))&&(l.disconnect(),t(o))});l.observe(document.body,{childList:!0,subtree:!0})}),t=(()=>{let e=document.getElementById("visual-log");return e||((e=document.createElement("div")).id="visual-log",e.style.position="fixed",e.style.bottom="0",e.style.left="0",e.style.zIndex="9999",e.style.backgroundColor="white",e.style.padding="1em",e.style.border="1px solid black",e.style.borderRadius="1em",e.style.margin="1em",e.style.maxHeight="50vh",e.style.maxWidth="50vw",e.style.overflowY="scroll",e.style.fontFamily="monospace",document.body.appendChild(e),e)})(),o=e=>{const o=document.createElement("div");o.innerText=e,o.style.opacity="1",o.style.transition="opacity 1s",o.style.marginBottom="0.5em",t.appendChild(o),setTimeout(()=>{o.style.opacity="0",setTimeout(()=>{t.removeChild(o)},1e3)},4e3)};let l;for(;l=document.querySelector('[role="article"]:has(> * > * > * > * > * > * > * > * > * > * > * > * > * > * > * > [aria-label="Verified account"] > g > path:not([clip-rule="evenodd"])) [aria-label="More"]');){const t=[...l.closest('[role="article"]').querySelector('[data-testid="User-Name"]').querySelectorAll('[role="link"]:not(:has(time))')].map(e=>e.innerText).join(" ");l.click(),(await e('[data-testid="block"]')).click(),(await e('[data-testid="confirmationSheetConfirm"]')).click(),o(`Blocked ${t}`),await new Promise(e=>setTimeout(e,800))}o("No more verified accounts to block")})();

You can then click the bookmark to run the script on any Twitter page. Keep in mind that the copy of the script in the bookmark will not be updated alongside this file, so you may want to check back here for updates.

Screenshot

If you don't trust the above bookmarklet, you can create your own by copying the code below into a JavaScript minifier, such as the one at https://skalman.github.io/UglifyJS-online/, and then pasting the minified code into the URL field of a new bookmark with javascript: at the beginning.

Finding Verified Accounts

You can locate tweets by verified accounts by searching for -filter:verified filter:blue_verified in the Twitter search bar.

Firefox Users

You need to first enable the :has() CSS pseudo-class in about:config. Browse to about:config, accept the warning, and search for layout.css.has-selector.enabled. If it is set to false, double-click it to change it to true.

Screenshot
This is what you should see when your settings are correct.

Disclaimer

This script is provided as-is, and is not guaranteed to work. It may stop working at any time if Twitter changes their website. It is your responsibility to ensure that you are using this script in accordance with Twitter's terms of service. Otherwise, you may experience rate limiting, account lockouts, or other issues I cannot foresee.

The Script

;(async () => {
  const waitSeconds = 0.8 // How long to wait in between blocking accounts, in seconds. If you're getting rate limited or logged out, try increasing this time span so that you're not making too many requests too quickly.
  const waitForElement = selector =>
    new Promise(resolve => {
      let element = document.querySelector(selector)
      if (element) {
        resolve(element)
        return
      }
      const observer = new MutationObserver(() => {
        element = document.querySelector(selector)
        if (element) {
          observer.disconnect()
          resolve(element)
        }
      })
      observer.observe(document.body, { childList: true, subtree: true })
    })

  const logMessage = message => {
    console.log(message);
  }
  
  let more
  while (
    (more = document.querySelector('[data-testid="tweet"] [aria-label="More"]'))
  ) {
    const username = [
      ...more
        .closest('[data-testid="tweet"]')
        .querySelector('[data-testid="User-Name"]')
        .querySelectorAll('[role="link"]:not(:has(time))')
    ]
      .map(el => el.innerText)
      .join(' ')
    more.click()
    ;(await waitForElement('[data-testid="block"]')).click()
    ;(await waitForElement('[data-testid="confirmationSheetConfirm"]')).click()
    logMessage(`Blocked ${username}`)
    await new Promise(resolve => setTimeout(resolve, waitSeconds * 1000))
  }
  logMessage('No more verified accounts to block')
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment