Last active
November 13, 2022 21:56
-
-
Save mikeyaworski/69034919262e3b027b1f88d1e6cba159 to your computer and use it in GitHub Desktop.
Auto Claim Twitch Channel Points (Mutation Observer)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Auto Claim Twitch Channel Points (Mutation Observer) | |
// @namespace http://tampermonkey.net/ | |
// @version 2.0.0 | |
// @description Auto Claim Twitch Channel Points | |
// @author https//github.com/mikeyaworski | |
// @match https://www.twitch.tv/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitch.tv | |
// @grant none | |
// ==/UserScript== | |
function log(...args) { | |
console.log('Auto Claim Twitch Channel Points:', ...args); | |
} | |
let observers = []; | |
let lastPointsSummarySection; | |
let lastClick = 0; | |
// This list gets updated live and is a performant way to create new mutation observers | |
const summaryClasses = document.getElementsByClassName('community-points-summary'); | |
function clickBonusButton() { | |
if (!summaryClasses[0]) return; | |
const bonusBtn = summaryClasses[0].querySelector('button[aria-label="Claim Bonus"]'); | |
// Add a 2 second delay between clicks | |
if (bonusBtn && Date.now() - lastClick > 2000) { | |
lastClick = Date.now(); | |
log('Claiming bonus', bonusBtn); | |
bonusBtn.click(); | |
} | |
} | |
function observeBonus() { | |
log('Creating mutation obvserver on points summary section', summaryClasses[0]); | |
lastPointsSummarySection = summaryClasses[0]; | |
clickBonusButton(); | |
observers.push( | |
new MutationObserver(() => { | |
clickBonusButton(); | |
}).observe(summaryClasses[0], { childList: true, subtree: true }), | |
); | |
} | |
function createObservers() { | |
if (summaryClasses[0]) observeBonus(); | |
log('Creating document mutation observer'); | |
observers.push( | |
new MutationObserver(() => { | |
if (summaryClasses[0] && summaryClasses[0] !== lastPointsSummarySection) { | |
observeBonus(); | |
} | |
}).observe(document.body, { subtree: true, childList: true }), | |
); | |
} | |
function teardownObservers() { | |
log('Clearing mutation observers', observers.length); | |
observers.forEach(observer => { | |
if (observer) observer.disconnect(); | |
}); | |
observers = []; | |
} | |
createObservers(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment