Greasy Fork

来自缓存

Greasy Fork is available in English.

X.com Auto-Following Tab

Automatically switch to "Following" instead of "For you" on X.com

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         X.com Auto-Following Tab
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Automatically switch to "Following" instead of "For you" on X.com
// @author       DiCK
// @match        https://x.com/*
// @match        https://twitter.com/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // Function to find and click the Following tab
    function switchToFollowing() {
        // Find all tab elements that contain "Following" or "For you"
        const tabs = document.querySelectorAll('a[role="tab"]');

        for (const tab of tabs) {
            const spanElement = tab.querySelector('span');
            if (spanElement && spanElement.textContent.trim() === 'Following') {
                // Check if Following tab is already active
                const isSelected = tab.getAttribute('aria-selected') === 'true';

                if (!isSelected) {
                    console.log('Switching to Following tab...');
                    tab.click();
                    return true;
                }
                return false; // Already on Following
            }
        }
        return false; // Didn't find Following tab
    }

    // Function to check if we're on the home page
    function isOnHomePage() {
        return window.location.pathname === '/' ||
               window.location.pathname === '/home' ||
               window.location.pathname.startsWith('/home');
    }

    // Main function that runs
    function runScript() {
        if (!isOnHomePage()) {
            return;
        }

        // Try switching immediately
        if (switchToFollowing()) {
            return;
        }

        // If that didn't work, wait and try again
        let attempts = 0;
        const maxAttempts = 20;

        const observer = new MutationObserver(function(mutations) {
            attempts++;

            if (attempts > maxAttempts) {
                observer.disconnect();
                return;
            }

            if (switchToFollowing()) {
                observer.disconnect();
            }
        });

        // Observe changes in DOM
        observer.observe(document.body, {
            childList: true,
            subtree: true
        });

        // Stop observer after 10 seconds
        setTimeout(() => {
            observer.disconnect();
        }, 10000);
    }

    // Run script when page loads
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', runScript);
    } else {
        // Wait a bit to let X.com finish loading
        setTimeout(runScript, 1000);
    }

    // Listen for URL changes (for SPA navigation)
    let currentUrl = window.location.href;

    const urlObserver = new MutationObserver(() => {
        if (window.location.href !== currentUrl) {
            currentUrl = window.location.href;
            setTimeout(runScript, 1500); // Wait a bit longer for navigation
        }
    });

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

    // Backup: Also run when user clicks on home link
    document.addEventListener('click', function(e) {
        const link = e.target.closest('a');
        if (link && (link.href.includes('/home') || link.href.endsWith('/'))) {
            setTimeout(runScript, 2000);
        }
    });

})();