Skip to content

Instantly share code, notes, and snippets.

@josh-richardson
Created March 12, 2020 17:41
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 josh-richardson/cc46f3d933e0878073ad5615bbf86cbd to your computer and use it in GitHub Desktop.
Save josh-richardson/cc46f3d933e0878073ad5615bbf86cbd to your computer and use it in GitHub Desktop.
Add a button to easily block your followers to twitter
let fetchToken = async () => {
let mainUrl = null;
for (let script of document.body.querySelectorAll("script[src]"))
if (/\/main\.[^\/]*\.js$/.test(script.src))
mainUrl = script.src;
if (!mainUrl)
return null;
let response = await fetch(mainUrl);
let mainSource = await response.text();
let result = /\"AAAAAAA[^"]+\"/.exec(mainSource);
if (!result || result.length != 1)
return null;
return JSON.parse(result[0]);
}
let authToken = await fetchToken();
let getCookie = (cname) => {
const name = `${cname}=`;
const decodedCookie = decodeURIComponent(document.cookie);
const ca = decodedCookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
let blockUser = async (userName) => {
return await fetch("https://api.twitter.com/1.1/blocks/create.json", {
"credentials": "include",
"referrer": "https://api.twitter.com/1.1/blocks/create.json",
"body": `screen_name=${userName}`,
"method": "POST",
"mode": "cors",
"headers": {
"x-twitter-auth-type": "OAuth2Session",
"x-twitter-client-language": "en",
"x-twitter-active-user": "yes",
"x-csrf-token": getCookie("ct0"),
"authorization": `Bearer ${authToken}`,
"Content-Type": "application/x-www-form-urlencoded",
}
});
}
let buttonClick = async (e) => {
let blockResult = await blockUser(e.currentTarget.dataset.blockUser);
if (blockResult.status === 200) {
alert("User blocked successfully.");
} else {
alert("The block operation failed, see the network tab for detailes of the failure.");
}
}
let createFromHtml = (htmlString) => {
var div = document.createElement('div');
div.innerHTML = htmlString.trim();
return div.firstChild;
}
Array.from(document.getElementsByTagName("span")).filter(i => (i.innerText === "Follow") && window.getComputedStyle(i).display !== 'none' && i.children.length === 0).forEach(i => {
let containerElement = i.parentElement.parentElement.parentElement.parentElement.parentElement;
let profileName = containerElement.children[0].children[0].getAttribute("href").substring(1);
let buttonHtml = `
<a class="block-button"
data-block-user="${profileName}"
style="display: block;margin-left: auto;border: 1px solid rgb(29, 161, 242);border-radius: 50%;height: 28px;width: 28px;">
<svg version="1.1" style="margin: 6px;" id="Capa_1"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 468.293 468.293" style="height: 16px;width: 16px;" xml:space="preserve">
<path style="fill:#1da1f2;" d="M234.146,0C104.898,0,0,104.898,0,234.146s104.898,234.146,234.146,234.146 s234.146-104.898,234.146-234.146S363.395,0,234.146,0z M66.185,234.146c0-93.034,75.551-168.585,167.961-168.585 c34.966,0,68.059,10.615,94.907,29.346L95.532,329.054C76.8,302.205,66.185,269.112,66.185,234.146z M234.146,402.107 c-34.966,0-68.059-10.615-94.907-29.346l233.522-233.522c18.732,26.849,29.346,59.941,29.346,94.907 C402.107,327.18,327.18,402.107,234.146,402.107z"></path>
</svg>
</a>
`;
let button = createFromHtml(buttonHtml);
button.onclick = buttonClick;
containerElement.children[0].insertAdjacentElement("afterEnd", button);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment