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

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

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

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

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

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

})();