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 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
})();