Greasy Fork is available in English.
Adds a block button to each reply and sub-reply on X, including when navigating to sub-replies. You must manually add your bearer token as called out below in order for this to work. If you're not sure what that is and how to get it, you probably should not use this script as there are risks involved when you do this.
当前为
// ==UserScript==
// @name X Block
// @namespace http://tampermonkey.net/
// @version 0.3
// @description Adds a block button to each reply and sub-reply on X, including when navigating to sub-replies. You must manually add your bearer token as called out below in order for this to work. If you're not sure what that is and how to get it, you probably should not use this script as there are risks involved when you do this.
// @author adamlproductions
// @match https://x.com/*
// @grant GM_notification
// @license MIT
// ==/UserScript==
(function() {
'use strict';
// Manually added bearer token (replace with your actual token)
const bearerToken = 'PASTE YOUR BEARER TOKEN HERE';
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}
function blockUser(username, tweetElement) {
let screenName = `screen_name=${username}`;
let ct0 = getCookie('ct0');
const headers = {
'authorization': `Bearer ${bearerToken}`,
'Content-Type': 'application/x-www-form-urlencoded',
'x-csrf-token': ct0
};
fetch('/i/api/1.1/blocks/create.json', {
method: 'POST',
headers: headers,
body: screenName,
credentials: 'include'
})
.then(response => {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
})
.then(data => {
GM_notification({
text: `User ${username} was blocked.`,
title: 'X Block',
tag: 'XBlockTag',
timeout: 3000,
silent: true,
url: 'https:/example.com/',
onclick: (event) => {
event.preventDefault();
}
});
if (tweetElement) {
tweetElement.style.display = 'none';
}
})
.catch(error => console.error('Error:', error));
}
function addBlockButton(article) {
const actions = article.querySelector('div[role="group"]');
if (actions && !actions.querySelector('.block-button')) {
const usernameLink = article.querySelector('a[href^="/"]');
if (usernameLink) {
const screenName = usernameLink.getAttribute('href').slice(1);
const blockButton = document.createElement('button');
blockButton.textContent = 'Block';
blockButton.className = 'block-button';
blockButton.style.marginLeft = '10px';
blockButton.style.cursor = 'pointer';
blockButton.addEventListener('click', () => {
blockUser(screenName, article);
});
actions.appendChild(blockButton);
}
}
}
function scanAndAddButtons() {
document.querySelectorAll('article').forEach(article => {
addBlockButton(article);
});
}
let pageObserver;
function observePage() {
if (pageObserver) pageObserver.disconnect();
const contentArea = document.querySelector('main') || document.body;
pageObserver = new MutationObserver(() => {
scanAndAddButtons();
});
pageObserver.observe(contentArea, { childList: true, subtree: true });
scanAndAddButtons();
}
let lastPath = '';
function checkNavigation() {
const currentPath = window.location.pathname;
if (currentPath !== lastPath) {
lastPath = currentPath;
if (/\/status\/\d+/.test(currentPath)) {
observePage();
} else {
if (pageObserver) {
pageObserver.disconnect();
pageObserver = null;
}
}
}
}
setInterval(checkNavigation, 500);
checkNavigation();
})();