Greasy Fork

Greasy Fork is available in English.

SetXHomeIsFollowing

强制 X.com 默认显示 Following 标签页

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         SetXHomeIsFollowing
// @description  强制 X.com 默认显示 Following 标签页
// @match        https://x.com/*
// @noframes
// @inject-into  content
// @version      0.0.21
// @grant        none
// @namespace http://greasyfork.icu/users/1548986
// ==/UserScript==

(function() {
    'use strict';

    let hasSwitched = false;

    const trySwitchToFollowing = () => {
        // 如果已经成功切换过,或者当前不是在首页路径,就跳过
        if (hasSwitched || !window.location.href.includes('/home')) return;

        // 寻找包含 "Following" 或 "正在关注" 的 Tab
        // 考虑到多语言环境,我们通过属性和文字深度筛选
        const tabs = document.querySelectorAll('div[role="tab"]');

        tabs.forEach(tab => {
            const text = tab.innerText || "";
            // 匹配英文 "Following" 或中文 "正在关注"
            if (text.includes('Following') || text.includes('正在关注')) {
                const isSelected = tab.getAttribute('aria-selected') === 'true';

                if (isSelected) {
                    // 如果已经是 Following 状态,标记完成并停止
                    hasSwitched = true;
                    return;
                }

                // 执行点击
                tab.click();
                hasSwitched = true;
                console.log('Successfully switched to Following tab.');
            }
        });
    };

    // 使用 MutationObserver 监听 DOM 变化
    const observer = new MutationObserver(() => {
        if (!hasSwitched) {
            trySwitchToFollowing();
        }
    });

    // 开始监听
    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 针对 SPA 路由跳转的补丁:当 URL 变化时重置状态
    let lastUrl = location.href;
    setInterval(() => {
        if (location.href !== lastUrl) {
            lastUrl = location.href;
            if (lastUrl.includes('/home')) {
                hasSwitched = false; // 重新回到首页时,允许再次触发切换
            }
        }
    }, 1000);

    // 10秒后自动停止观察以节省性能
    setTimeout(() => observer.disconnect(), 10000);
})();