Twitch Channel Points Chest Auto Opener

Automatically click channel points bonus button

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

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

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

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

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

您需要先安装一款用户脚本管理器扩展,例如 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();
})();