X Block (Fixed)

Adds a block button to tweets excluding your own, using native Twitter block

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Greasemonkey lub Violentmonkey.

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

Aby zainstalować ten skrypt, wymagana jest instalacje jednego z następujących rozszerzeń: Tampermonkey, Violentmonkey.

Aby zainstalować ten skrypt, wymagana będzie instalacja rozszerzenia Tampermonkey lub Userscripts.

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

Aby zainstalować ten skrypt, musisz zainstalować rozszerzenie menedżera skryptów użytkownika.

(Mam już menedżera skryptów użytkownika, pozwól mi to zainstalować!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Będziesz musiał zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

Musisz zainstalować rozszerzenie menedżera stylów użytkownika, aby zainstalować ten styl.

(Mam już menedżera stylów użytkownika, pozwól mi to zainstalować!)

// ==UserScript==
// @name         X Block (Fixed)
// @namespace    http://tampermonkey.net/
// @version      0.5
// @description  Adds a block button to tweets excluding your own, using native Twitter block
// @author       adamlproductions
// @match        https://x.com/*
// @run-at       document-end
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    let myScreenName = null;

    function extractUserInfo() {
        if (myScreenName) return;
        const profileLink = document.querySelector('a[href^="/"][aria-label*="Profile"]');
        if (profileLink) {
            const href = profileLink.getAttribute('href');
            if (href && href !== '/settings/profile') {
                myScreenName = href.slice(1).toLowerCase();
            }
        }
    }

    function triggerNativeBlock(tweetElement) {
        const menuButton = tweetElement.querySelector('button[aria-label="More"][role="button"]');
        if (!menuButton) return;

        menuButton.click();
        setTimeout(() => {
            const blockOption = document.querySelector(`div[role="menuitem"][data-testid="block"]`);
            if (blockOption) {
                blockOption.click();
            }
        }, 100);
    }

    function addBlockButton(tweet) {
        const actions = tweet.querySelector('div[role="group"]');
        if (actions && !actions.querySelector('.block-button')) {
            const usernameLink = tweet.querySelector('a[href^="/"]');
            if (usernameLink) {
                const screenName = usernameLink.getAttribute('href').slice(1).toLowerCase();

                if (screenName !== myScreenName) {
                    const blockButton = document.createElement('button');
                    blockButton.textContent = 'Block';
                    blockButton.className = 'block-button';
                    blockButton.setAttribute('style', 'margin-left:10px; color:rgb(249, 24, 128); cursor:pointer; font-size:13px; font-weight:bold; border:none; background:none;');

                    blockButton.addEventListener('click', (e) => {
                        e.stopPropagation();
                        triggerNativeBlock(tweet);
                    });
                    actions.appendChild(blockButton);
                }
            }
        }
    }

    function scanAndAddButtons() {
        extractUserInfo();
        document.querySelectorAll('article').forEach(article => {
            addBlockButton(article);
        });
    }

    const observer = new MutationObserver(() => {
        scanAndAddButtons();
    });

    function start() {
        const target = document.querySelector('body');
        if (target) {
            observer.observe(target, { childList: true, subtree: true });
            scanAndAddButtons();
        } else {
            setTimeout(start, 500);
        }
    }

    start();
})();