Twitter UI Cleanup

Remove unwanted buttons and sections on Twitter

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitter UI Cleanup
// @namespace    https://github.com/Daminator4113/Twitter-UI-Cleanup
// @version      1.4
// @author       Daminator4113
// @description  Remove unwanted buttons and sections on Twitter
// @license      MIT
// @icon         https://abs.twimg.com/favicons/twitter.2.ico
// @match        https://twitter.com/*
// @match        https://x.com/*
// ==/UserScript==

(function() {
    'use strict';

    const removeParent = (selector, levels, tagName) => {
        const element = document.querySelector(selector);
        if (element) {
            let parent = element;
            for (let i = 0; i < levels; i++) {
                parent = parent.parentElement;
                if (!parent) break;
            }
            if (parent?.tagName === tagName) {
                //console.log('REMOVE',parent);
                parent.remove();
            }
        }
    };

    // The order of removal is important!
    const removeElements = () => {
        const selectors = [
            'a[href*="/i/grok"]', // Grok
            'a[data-testid="premium-signup-tab"]', // Twitter Blue
            'a[data-testid="vo-signup-tab"]', // Verified organizations
            'button[data-testid="grokImgGen"]', // "Generate Image with Grok" button.
            'div.css-175oi2r.r-1s2bzr4.r-dnmrzs.r-bnwqim', // Grok questions suggestions
            'button[aria-label="Actions Grok"]', // Grok explain tweet button
        ];

        selectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => el.remove());
        });

        removeParent('a[href*="/i/premium_sign_up"]', 3, 'DIV'); // "Try Premium for free" section
        removeParent('div[data-testid="GrokDrawerHeader"]', 1, 'DIV'); // Grok Drawer → I don't know why, but removing the "GrokDrawer" div deactivate the autoplay for videos?

        const grokSVG = 'path[d="M2.205 7.423L11.745 21h4.241L6.446 7.423H2.204zm4.237 7.541L2.2 21h4.243l2.12-3.017-2.121-3.02zM16.957 0L9.624 10.435l2.122 3.02L21.2 0h-4.243zm.767 6.456V21H21.2V1.51l-3.476 4.946z"]';
        removeParent(grokSVG, 4, 'BUTTON'); // Grok profil resume
        removeParent(grokSVG, 5, 'BUTTON'); // Grok explain tweet
    };

    const observer = new MutationObserver(removeElements);

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    removeElements();
})();