Greasy Fork

Greasy Fork is available in English.

X Block

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.

当前为 2025-02-20 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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();
})();