Twitch Live Auto Reload (獨立版)

監控 Twitch 頻道頁面,當偵測到「正在開台」等通知時自動刷新頁面 (在所有 Twitch 頁面運行檢查)。

当前为 2025-11-30 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name                Twitch Live Auto Reload (獨立版)
// @name         Twitch 直播開始自動刷新 (獨立版)
// @version             1.1.0
// @description         監控 Twitch 頻道頁面,當偵測到「正在開台」等通知時自動刷新頁面 (在所有 Twitch 頁面運行檢查)。
// @match        https://www.twitch.tv/*
// @author       程式夥伴 (基於使用者原碼修改)
// @namespace    https://greasyfork.org/users/ianias2
// @license      MPL-2.0
 
// @grant        none
// @run-at       document-body
// ==/UserScript==
 
(function() {
    'use strict';

    // ====================================================================
    // ⚙️ 配置區塊 (Config)
    // ====================================================================

    const Config = {
        Dev: true, // 建議設為 false
        // 直播開始自動刷新的檢查間隔 (秒)。預設 3 秒檢查一次頁面。
        AutoLiveCheckInterval: 3, 
    };

    // ====================================================================
    // 🌟 LiveMonitor 核心類別 (直播狀態監控與刷新)
    // ====================================================================

    class LiveMonitor {
        constructor() {
            this.RELOAD_DELAY_SECONDS = 0; 
            
            this.TARGET_KEYWORDS = [
                "開台", 
                "正在開台", 
                "立即觀賞", 
                "正在實況"
            ];
            
            // ⭐ 最終極簡修正:只檢查 URL 是否以 Twitch 域名開頭
            // 腳本會在所有 Twitch 頁面 (包括 /directory, /settings 等) 運行監控
            this.isChannelPage = window.location.href.startsWith("https://www.twitch.tv");
            
            this.timer = null;
            this.reloadTimer = null; 
        }

        /**
         * 檢查頁面上是否存在目標文字,如果存在則停止監控並設置延遲刷新。
         */
        checkAndReloadByText() {
            if (this.reloadTimer) return false; 
            
            // 構建 XPath 的 OR 條件
            const orConditions = this.TARGET_KEYWORDS
                .map(keyword => `contains(text(), '${keyword}')`)
                .join(' or ');

            // 修正: 排除 <title> 標籤和播放器控制元件
            const excludedElements = ` and not(self::title) and not(ancestor::div[contains(@class, 'player-controls')]) and not(ancestor::div[contains(@data-a-target, 'player-control-wrapper')])`;
            
            // 最終的 XPath 表達式
            const xpathExpression = `//body//*[not(self::script) and not(self::style)${excludedElements} and (${orConditions})]`;
            
            const matchingElement = document.evaluate(
                xpathExpression,
                document,
                null,
                XPathResult.FIRST_ORDERED_NODE_TYPE,
                null
            ).singleNodeValue;
            
            const allKeywords = this.TARGET_KEYWORDS.join(' / ');

            if (matchingElement) { 
                // 1. 停止主要的監控循環
                if (this.timer) {
                    clearInterval(this.timer);
                    this.timer = null;
                }
                
                let matchedKeyword = this.TARGET_KEYWORDS.find(k => matchingElement.textContent.includes(k)) || "Unknown";
                
                if (Config.Dev) {
                    console.log(`✅ LiveMonitor: 偵測成功!關鍵字: [${matchedKeyword}]。元素:`, matchingElement);
                    console.warn(`🚀 偵測完成,頁面將立即刷新。`);
                }

                // 2. 設置立即刷新
                this.reloadTimer = setTimeout(() => {
                    location.reload(); 
                }, this.RELOAD_DELAY_SECONDS * 1000); 
                
                return true; 
            }
            if (Config.Dev) console.log(`LiveMonitor: 未偵測到任何關鍵字:${allKeywords}`);
            return false;
        }

        /**
         * 啟動定時監控
         */
        async run() {
            if (this.timer) {
                clearInterval(this.timer);
                this.timer = null;
            }

            // 檢查修正: 寬鬆的檢查 (始終為 true,因為 @match 已經過濾了)
            if (!this.isChannelPage) {
                // 實際上這行代碼不太可能運行,因為 @match 和 this.isChannelPage 已經確保是 Twitch 網頁
                if (Config.Dev) console.log("LiveMonitor: 不在 Twitch 網站上,停止監控。"); 
                return;
            }
            
            if (Config.Dev) console.log(`LiveMonitor: 頻道監控啟動 (全 Twitch 網站),間隔: ${Config.AutoLiveCheckInterval}s,準備捕捉關鍵字: ${this.TARGET_KEYWORDS.join(', ')}`);

            // 3. 開始定時循環檢查文字
            this.timer = setInterval(() => {
                this.checkAndReloadByText();
            }, Config.AutoLiveCheckInterval * 1000);
        }
    }

    // ====================================================================
    // 🚀 腳本啟動
    // ====================================================================

    // 實例化 LiveMonitor 並啟動監控
    const liveMonitor = new LiveMonitor();
    liveMonitor.run(); 

})();