Twitch Channel Points Chest Auto Opener

Automatically click channel points bonus button

目前為 2021-01-03 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name Twitch Channel Points Chest Auto Opener
// @name:ja Twitchポイントチェスト自動開封
// @version 1.01
// @author N.Y.Boyu
// @description Automatically click channel points bonus button
// @description:ja 自動でチャンネルポイントのボーナスボタンをクリックします
// @match https://www.twitch.tv/*
// @match https://dashboard.twitch.tv/*
// @license MIT
// @grant none
// @namespace https://greasyfork.org/users/175598
// ==/UserScript==
(function(){
    if(MutationObserver&&history.pushState)console.log("CAO: Chest Auto Opener is enabled.");else return;

    const INITIAL_STANDBY=10000; //default: 10,000ms
    const INITIAL_RECALL=300000; //default: 300,000ms
    const CHEST_WAIT=895000; //default: 895,000ms
    //NOTE: Chest appears each 15 minutes. (help.twitch.tv/s/article/channel-points-guide)

    //avoid multi-thread
    let timer=-1;
    let setTimer=function(func,wait){
        clearTimeout(timer);
        timer=setTimeout(func,wait);
    };

    let container;
    let waiting=false;
    let check=function(){
        if(waiting)return;
        let bonus = container.getElementsByClassName("claimable-bonus__icon")[0];
        if(bonus){
            bonus.click();
            console.log("CAO: Chest opened at "+new Date());
            waiting=true;
            setTimer(function(){waiting=false;check();},CHEST_WAIT);
        }
    };

    let observer=new MutationObserver(check);
    let initCheck=function(){
        container=document.getElementsByClassName("community-points-summary")[0];
        if(container){
            console.log("CAO: Channel points exist");
            observer.observe(container,{childList:true,subtree:true});
            check();
        }else{
            console.log("CAO: Channel points dosen't exist");
            if(INITIAL_RECALL>0)setTimer(initCheck,INITIAL_RECALL);
        }
    };

    //detect loaction change
    let init=function(){
        console.log("CAO: location change detected at "+new Date());
        waiting=false;
        observer.disconnect();
        setTimer(initCheck,INITIAL_STANDBY);
    };
    history.pushState=(f=>function(){let res=f.apply(this,arguments);init();return res;})(history.pushState);
    history.replaceState=(f=>function(){let res=f.apply(this,arguments);init();return res;})(history.replaceState);
    window.addEventListener('popstate',init);
    init();
})();