Twitch Channel Points Chest Auto Opener

Automatically click channel points bonus button

当前为 2021-01-03 提交的版本,查看 最新版本

// ==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();
})();