Twitch Stream Auto-Watch

Auto-Watches a stream that is offline on page load, as soon as it goes online.

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Twitch Stream Auto-Watch
// @namespace    https://greasyfork.org/en/users/1077259-synthetic
// @version      0.7
// @description  Auto-Watches a stream that is offline on page load, as soon as it goes online.
// @author       @Synthetic
// @license      MIT
// @match        https://www.twitch.tv/*
// @exclude      https://www.twitch.tv/inventory
// @exclude      https://www.twitch.tv/drops/inventory
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitch.tv
// ==/UserScript==

(function() {
    'use strict';

    // The current version
    const VERSION = 0.7;

    // Page element selectors
    const STATUS_OFFLINE = 'div.channel-status-info--offline';
    const WATCH_NOW = 'div.channel-status-info--live';

    /**
     * Clicks the Watch link.
     *
     * @return boolean
     */
    const clickWatch = () => {
        if (document.querySelector(WATCH_NOW)) {
            document.querySelector(WATCH_NOW).click();
            return true;
        }
        return false;
    };

    const onMutate = function(mutationsList) {
        const offline = document.querySelectorAll(STATUS_OFFLINE);
        if (offline.length) {
            clearTimeout(timeout);
            if (!waiting) {
                console.log('Stream is offline. Waiting');
                waiting = true;
            }
        }
        const online = document.querySelectorAll(WATCH_NOW);
        if (online.length) {
            console.log('Stream is online');
            if (clickWatch()) {
                observer.disconnect();
                console.log('Watching');
            }
        }
    };

    console.log('Loaded at', new Date());

    var waiting = false;
    var timeout = window.setTimeout(
        () => {
            observer.disconnect();
        },
        10000
    );

    var observer = new MutationObserver(onMutate);
    observer.observe(document.body, { childList: true, subtree: true });

})();