Twitch Channel Points Claimer

Automatically claim channel points. Based on original script by PartMent

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name Twitch Channel Points Claimer
// @version 2.3
// @author JakeBathman
// @description Automatically claim channel points. Based on original script by PartMent
// @match https://www.twitch.tv/*
// @match https://dashboard.twitch.tv/*
// @license MIT
// @grant none
// @namespace https://github.com/jakebathman/Twitch-Channel-Points-Claimer
// ==/UserScript==

let MutationObserver =
    window.MutationObserver ||
    window.WebKitMutationObserver ||
    window.MozMutationObserver;
let claiming = false;
let channelName =
    window.location.pathname.substring(
        window.location.pathname.lastIndexOf('/') + 1
    ) || '';
let localStorageKey = `autoClaimer_count_${channelName}`;
let localStorageKeyGlobal = `autoClaimer_count_global`;
let localStorageKeyLastSpecialBonus = `autoClaimer_lastSpecialBonus_${channelName}`;

let claimCount = localStorageGet(localStorageKey);
let claimCountGlobal = getGlobalCount();

let lastSpecialBonus;

console.log('############ Global count: ', claimCountGlobal);

var addedCountElement = false;

function addCountEl() {
    console.log('Trying to add count');

    try {
        console.log('############ adding count');
        let cpButton = document.querySelector(
            '.chat-input__buttons-container div:first-of-type'
        );
        var countEl = document.createElement('div');
        countEl.className = 'autoClaimer_count tw-flex';
        countEl.style.cssText = 'margin: auto;color: #9147ff;';
        countEl.innerHTML = getCountHtml();
        cpButton.parentNode.insertBefore(countEl, cpButton.nextSibling);

        addedCountElement = true;
    } catch (e) {
        console.log('Waiting 1s', addedCountElement);
    }
    if (!addedCountElement) {
        setTimeout(() => {
            addCountEl();
        }, 1000);
    }
}
addCountEl();

// Try to load the last bonus time from localStorage (prevents ?m on refresh)
if (localStorage.hasOwnProperty(localStorageKeyLastSpecialBonus)) {
    var storedLastSpecialBonus = new Date(parseInt(
        localStorage.getItem(localStorageKeyLastSpecialBonus)
    ));
    if ((new Date() - storedLastSpecialBonus) / (1000 * 60) > 15) {
        // This is an old one, so nuke it from localStorage
        localStorage.removeItem(localStorageKeyLastSpecialBonus);
    } else {
        lastSpecialBonus = storedLastSpecialBonus;
    }
}
setInterval(updateCountHtml, 1000);

if (MutationObserver) {
    console.log('Auto claimer is enabled.');
}

let observer = new MutationObserver((e) => {
    try {
        let bonus = document.querySelector('.claimable-bonus__icon');
        if (bonus && !claiming) {
            console.log('Auto claimer: button added', bonus);
            bonus.click();
            claiming = true;

            lastSpecialBonus = new Date();
            localStorage.setItem(
                localStorageKeyLastSpecialBonus,
                lastSpecialBonus.getTime()
            );

            claimCount = localStorageInc(localStorageKey);
            claimCountGlobal = localStorageInc(localStorageKeyGlobal);

            // After a second or so, revert the claimed status back
            // to false to keep waiting for the next button
            let date = new Date();
            setTimeout(() => {
                console.log('Claimed at ' + date);
                claiming = false;
            }, Math.random() * 1000 + 2000);
        }
    } catch (error) {
        console.error('#### AutoClaimer error!', error);
    }
});

observer.observe(document.body, { childList: true, subtree: true });

function updateCountHtml() {
    var countEl = document.querySelector('.autoClaimer_count');
    countEl.innerHTML = getCountHtml();
}

function getCountHtml() {
    return `<div style="padding-right: 4px;font-weight:bold;">${getTimeSinceSpecialBonus()}</div><div style="padding-right: 4px;">•</div> <div style="padding-right: 4px;">${claimCount}</div> <div>(${claimCountGlobal})</div>`;
}

function getTimeSinceSpecialBonus() {
    if (!lastSpecialBonus) {
        return '?m';
    }

    let nextSpecialBonus = addMinutes(lastSpecialBonus, 15);
    let diffMinutes = (nextSpecialBonus - new Date()) / 1000 / 60;

    if (diffMinutes < 1) {
        return '<1m';
    }

    return Math.round(diffMinutes) + 'm';
}

function addMinutes(date, minutes) {
    return new Date(date.getTime() + minutes * 60000);
}

function localStorageGet(key) {
    if (!localStorage.hasOwnProperty(key)) {
        // Add the localStorage key for this channel and set to 0
        localStorage.setItem(key, 0);
    }

    return parseInt(localStorage.getItem(key)) || 0;
}

// Add helper increment a localStorage count
function localStorageInc(key) {
    // Get the current value as int
    let value = localStorageGet(key);

    // Add one
    value += 1;

    // Set again
    localStorage.setItem(key, value);

    return value;
}

function getGlobalCount() {
    let globalCount = 0;

    for (let k in localStorage) {
        if (
            k.indexOf('autoClaimer_count_') > -1 &&
            k !== localStorageKeyGlobal
        ) {
            globalCount += parseInt(localStorage.getItem(k));
        }
    }

    localStorage.setItem(localStorageKeyGlobal, globalCount);

    return globalCount;
}