Skip to content

Instantly share code, notes, and snippets.

@mgwalker
Created December 26, 2022 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgwalker/21c04e2c7fdd0d9c34af032d2c9bb561 to your computer and use it in GitHub Desktop.
Save mgwalker/21c04e2c7fdd0d9c34af032d2c9bb561 to your computer and use it in GitHub Desktop.
Block lots of Mastodon instances, quickly

If you're a Mastodon moderator looking to quickly suspend a bunch of other instances from federation, here's some code that'll let you do it as a batch. It requires interating with your browser's Javascript console.

  1. Open your web browser to your Mastodon instance and log in
  2. Open your browser's Javascript console.
    • Safari: command-option-c
    • Firefox: command-option-k
    • Chrome: command-option-j
  3. Copy block.js below, edit lines 5 and 27 to point to your own instance (replace [your-instance-domain]), and then paste that into the console and press enter. (There should be an input box at the bottom of the console window.)
    • You can also edit line 22 if you want to add an internal-only message about why the domains are being blocked. If you don't want a message, change line 22 to just "".
  4. Turn your list of domains to block into a JSON array
    • It should look like this: ["domain 1", "domain 2", "domain 3", etc.]
    • The list begins with a [ and ends with a ]
    • The items in the list are separated by commas
    • Each item in the list must be quoted
  5. Run the blocker by typing into your browser console: block(["domain 1", "domain 2", "domain 3", etc.])
    • You can copy+paste your list from step 5 in between the ( and ) above
  6. Wait. It'll print out each domain as it gets blocked, and it'll print done when it has finished them all. Don't close or reload the page while it's working!
const block = async (domains) => {
for await (const domain of domains) {
console.log(`blocking ${domain}...`);
const key = await fetch(
"https://[your-instance-domain]/admin/domain_blocks/new"
)
.then((v) => v.text())
.then((t) =>
t.match(/<input .*?name="authenticity_token".*?value="([^"]+)"/)
)
.then(([, key]) => key);
const body = new URLSearchParams();
body.append("authenticity_token", key);
body.append("domain_block[domain]", domain);
body.append("domain_block[severity]", "suspend");
body.append("domain_block[reject_media]", 0);
body.append("domain_block[reject_reports]", 0);
body.append("domain_block[obfuscate]", 0);
body.append(
"domain_block[private_comment]",
"[your reason for blocking here]"
);
body.append("domain_block[public_comment]", "");
body.append("button", "");
await fetch("https://[your-instance-domain]/admin/domain_blocks", {
method: "POST",
body,
});
}
console.log("done!");
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment