Hide Twitter(X) Premium Sections

Hides Twitter Blue sections and fixes GIF issue

目前為 2025-02-12 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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
// @namespace    https://x.com/mirabella_777
// @version      1.0
// @description  Hides Twitter Blue sections and fixes GIF issue
// @author       mirabella_777
// @match        *://twitter.com/*
// @match        *://x.com/*
// @grant        none
// @run-at       document-idle
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    let isEnabled = true; // Toggle state

    const premiumSelectors = [
        '[data-testid="verified"]', // Blue check users
        '[aria-label="Verified"]', // Verified badge on profiles
        '[href="/i/verified-orgs-signup"]', // Verified organizations
        '[href="/i/premium_sign_up"]', // Verified organizations
        '[href="/jobs"]', // Jobs
        '[aria-label="Top Articles"]', // Twitter Blue 'Top Articles'
        'a[href="/i/premium"]', // Premium tab on the left sidebar
        'div[role="button"]:has(span)', // Some premium buttons
        'div[aria-label="Timeline: Verified"]', // Verified timeline
        '.r-vacyoi > div:nth-child(3)', // Verified timeline
        'div[aria-label="Subscribe to Premium"]', // Subscribe to Premium section
    ];

    function hidePremiumContent() {
        if (!isEnabled) return;

        // Hide premium content
        premiumSelectors.forEach(selector => {
            document.querySelectorAll(selector).forEach(el => {
                el.style.display = 'none';
                el.setAttribute('data-hidden-premium', 'true');
            });
        });
    }

    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'; // Match size of "Post" button
        toggleButton.style.margin = '10px 0';
        toggleButton.style.borderRadius = '50px'; // Rounded corners like the "Post" button
        toggleButton.style.fontSize = '14px';
        toggleButton.style.fontWeight = 'bold'; // Similar font weight as "Post"
        toggleButton.style.fontFamily = '"Helvetica Neue", Helvetica, Arial, sans-serif'; // Use X's font
        toggleButton.style.color = '#fff';
        toggleButton.style.backgroundColor = '#1D9BF0'; // Match the "Post" button color
        toggleButton.style.border = '1px solid #1D9BF0'; // Border like "Post"
        toggleButton.style.display = 'inline-block';
        toggleButton.style.textAlign = 'center';
        toggleButton.style.boxSizing = 'border-box';
        toggleButton.style.transition = 'background-color 0.2s ease'; // Smooth transition on hover
        toggleButton.onclick = toggleScript;

        // Hover effect (like the "Post" button)
        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";
        }
    }

    // Ensure GIFs are never hidden
    function ensureGifVisibility() {
        document.querySelectorAll('video[aria-label="GIF"]')?.forEach(gif => {
            gif.style.visibility = 'visible'; // Make sure GIFs remain visible
        });
    }

    document.addEventListener('DOMContentLoaded', () => {
        addToggleButton();
        hidePremiumContent();
        ensureGifVisibility(); // Ensure GIFs are visible on page load
    });

    // Observe for dynamically loaded content or navigation changes
    const observer = new MutationObserver(() => {
        hidePremiumContent();
        addToggleButton();
        ensureGifVisibility(); // Ensure GIFs remain visible on navigation or new content
    });
    observer.observe(document.body, { childList: true, subtree: true });

})();