-
-
Save tizee/495e3411d6311c12ea3c06c4bf3c0bcf to your computer and use it in GitHub Desktop.
Twitter insta block
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
// ==UserScript== | |
// @name Twitter Insta Block with Firefox support | |
// @namespace your-namespace-here | |
// @version 1.1 | |
// @description Adds a "Block User" button on Twitter timeline conversations and blocks the user with one click | |
// @author zoubingwu, tizee | |
// @match https://twitter.com/* | |
// @grant none | |
// ==/UserScript== | |
(function () { | |
"use strict"; | |
function getCookie(name) { | |
const cookieString = document.cookie; | |
const cookies = cookieString.split(";"); | |
const cookieMap = cookies.reduce((map, cookie) => { | |
const [key, value] = cookie.trim().split("="); | |
map[key] = value; | |
return map; | |
}, {}); | |
return cookieMap[name]; | |
} | |
function getHeader() { | |
// Provide your own token here | |
const headers = { | |
authorization: "", | |
"x-csrf-token": getCookie("ct0"), | |
"x-twitter-auth-type": "OAuth2Session", | |
"x-twitter-active-user": "yes", | |
"x-twitter-client-language": "en", | |
}; | |
return headers; | |
} | |
async function getUserId(screenName) { | |
return fetch( | |
`https://twitter.com/i/api/1.1/users/show.json?screen_name=${screenName}`, | |
{ | |
method: "GET", | |
credentials: "include", | |
headers: getHeader(), | |
} | |
) | |
.then((res) => res.json()) | |
.then((data) => { | |
console.log(`User @${screenName} info`, data); | |
return data.id_str; | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
} | |
// Define a function to block a user by screen name | |
async function blockUser(screenName) { | |
const userId = await getUserId(screenName); | |
fetch( | |
`https://twitter.com/i/api/1.1/blocks/create.json?screen_name=${screenName}`, | |
{ | |
method: "POST", | |
credentials: "include", | |
headers: getHeader(), | |
body: { | |
user_id: userId, | |
}, | |
} | |
) | |
.then((response) => { | |
if (response.ok) { | |
console.log(`User @${screenName} blocked successfully`); | |
} else { | |
console.error(`Failed to block user @${screenName}`); | |
} | |
}) | |
.catch((error) => { | |
console.error(error); | |
}); | |
} | |
const regex = /^https:\/\/twitter\.com\/\w+\/status\/(\d+)$/; | |
const style = document.createElement("style"); | |
style.innerHTML = ` | |
.insta-block-button { | |
margin-right: 10px; | |
height: 36px; | |
width: 36px; | |
border-radius: 50%; | |
display: flex; | |
align-items: center; | |
justify-content: center; | |
cursor: pointer; | |
transition-duration: 0.2s; | |
} | |
.insta-block-button:hover { | |
background-color: rgba(29, 155, 240, 0.1) | |
} | |
`; | |
document.body.appendChild(style); | |
const tampered = new WeakSet(); | |
const observer = new MutationObserver(function (mutationList, observer) { | |
if (!regex.test(window.location.href)) { | |
return; | |
} | |
// Find the screen name of the user who posted the conversation | |
const conversationItems = document.querySelectorAll( | |
'[data-testid="tweet"]' | |
); | |
// For each conversation item, add a "Block User" button | |
conversationItems.forEach((item, index) => { | |
if (index === 0) { | |
return; | |
} | |
if (tampered.has(item)) { | |
return; | |
} | |
const screenName = item | |
.querySelector('[data-testid="User-Name"] a[role="link"]') | |
.getAttribute("href") | |
.substring(1); | |
if (item.querySelector(`button[data-name="${screenName}"]`)) { | |
return; | |
} | |
// Create a "Block User" button | |
const blockButton = document.createElement("div"); | |
blockButton.innerHTML = `<svg viewBox="0 0 24 24" aria-hidden="true" class="r-18jsvk2 r-4qtqp9 r-yyyyoo r-1q142lx r-1xvli5t r-dnmrzs r-bnwqim r-1plcrui r-lrvibr"><g><path d="M12 3.75c-4.55 0-8.25 3.69-8.25 8.25 0 1.92.66 3.68 1.75 5.08L17.09 5.5C15.68 4.4 13.92 3.75 12 3.75zm6.5 3.17L6.92 18.5c1.4 1.1 3.16 1.75 5.08 1.75 4.56 0 8.25-3.69 8.25-8.25 0-1.92-.65-3.68-1.75-5.08zM1.75 12C1.75 6.34 6.34 1.75 12 1.75S22.25 6.34 22.25 12 17.66 22.25 12 22.25 1.75 17.66 1.75 12z"></path></g></svg>`; | |
blockButton.dataset.name = screenName; | |
blockButton.classList.add("insta-block-button"); | |
// Add a click event listener to the button to block the user | |
blockButton.addEventListener("click", (e) => { | |
e.preventDefault(); | |
e.stopPropagation(); | |
// Firfox compatiblility | |
const tweet = document | |
.querySelector(`div.insta-block-button[data-name="${screenName}"]`) | |
.parentElement.closest(`div[data-testid="cellInnerDiv"]`); | |
if (tweet) { | |
tweet.remove(); | |
} | |
blockUser(screenName); | |
}); | |
// Add the "Block User" button to the conversation item | |
const menu = item.querySelector('div[aria-label="More"]'); | |
if (menu) { | |
menu.parentNode.prepend(blockButton); | |
tampered.add(item); | |
} | |
}); | |
}); | |
observer.observe(document.documentElement, { | |
childList: true, | |
subtree: true, | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment