Hide Twitter(X) Premium Sections & Block Elon Musk

Hides Twitter Blue sections, fixes GIF issue, and redirects from Elon Musk's profile and posts

当前为 2025-03-08 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Hide Twitter(X) Premium Sections & Block Elon Musk
// @namespace    https://x.com/mirabella_777
// @version      1.1
// @description  Hides Twitter Blue sections, fixes GIF issue, and redirects from Elon Musk's profile and posts
// @author       mirabella_777
// @match        *://twitter.com/*
// @match        *://x.com/*
// @grant        none
// @run-at       document-idle
// ==/UserScript==

(function() {
    'use strict';

    let isEnabled = true; // Toggle state

    const premiumSelectors = [
        '[data-testid="verified"]',
        '[aria-label="Verified"]',
        '[href="/i/verified-orgs-signup"]',
        '[href="/i/premium_sign_up"]',
        '[href="/jobs"]',
        '[aria-label="Top Articles"]',
        'a[href="/i/premium"]',
        'div[role="button"]:has(span)',
        'div[aria-label="Timeline: Verified"]',
        '.r-vacyoi > div:nth-child(3)',
        'div[aria-label="Subscribe to Premium"]',
    ];

    function hidePremiumContent() {
        if (!isEnabled) return;
        premiumSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                el.style.display = 'none';
                el.setAttribute('data-hidden-premium', 'true');
            });
        });
    }

    function redirectFromElon() {
        const elonUsername = "@elonmusk";
        const elonProfile = "/elonmusk";

        if (window.location.pathname.includes(elonProfile)) {
            window.location.href = "/home";
        }

        document.querySelectorAll('a[href*="elonmusk"]').forEach(link => {
            link.closest('article')?.remove(); // Remove tweets by Elon Musk
        });
    }

    function restorePremiumContent() {
        document.querySelectorAll('[data-hidden-premium]').forEach(el => {
            el.style.display = '';
            el.removeAttribute('data-hidden-premium');
        });
    }

    function toggleScript() {
        isEnabled = !isEnabled;
        if (isEnabled) {
            hidePremiumContent();
        } else {
            restorePremiumContent();
        }
        updateToggleButton();
    }

    function addToggleButton() {
        const sidebar = document.querySelector('nav[aria-label="Primary"]');
        if (!sidebar || document.getElementById('togglePremiumBtn')) return;

        let toggleButton = document.createElement('button');
        toggleButton.id = 'togglePremiumBtn';
        toggleButton.innerText = isEnabled ? "Premium Hidden" : "Premium Visible";
        toggleButton.style.cursor = 'pointer';
        toggleButton.style.padding = '8px 16px';
        toggleButton.style.margin = '10px 0';
        toggleButton.style.borderRadius = '50px';
        toggleButton.style.fontSize = '14px';
        toggleButton.style.fontWeight = 'bold';
        toggleButton.style.color = '#fff';
        toggleButton.style.backgroundColor = '#1D9BF0';
        toggleButton.style.border = '1px solid #1D9BF0';
        toggleButton.style.display = 'inline-block';
        toggleButton.onclick = toggleScript;

        toggleButton.onmouseover = () => {
            toggleButton.style.backgroundColor = '#1A8CD8';
            toggleButton.style.borderColor = '#1A8CD8';
        };
        toggleButton.onmouseout = () => {
            toggleButton.style.backgroundColor = '#1D9BF0';
            toggleButton.style.borderColor = '#1D9BF0';
        };

        sidebar.appendChild(toggleButton);
    }

    function updateToggleButton() {
        const toggleButton = document.getElementById('togglePremiumBtn');
        if (toggleButton) {
            toggleButton.innerText = isEnabled ? "Premium Hidden" : "Premium Visible";
        }
    }

    function ensureGifVisibility() {
        document.querySelectorAll('video[aria-label="GIF"]')?.forEach(gif => {
            gif.style.visibility = 'visible';
        });
    }

    document.addEventListener('DOMContentLoaded', () => {
        addToggleButton();
        hidePremiumContent();
        ensureGifVisibility();
        redirectFromElon();
    });

    const observer = new MutationObserver(() => {
        hidePremiumContent();
        addToggleButton();
        ensureGifVisibility();
        redirectFromElon();
    });
    observer.observe(document.body, { childList: true, subtree: true });

})();