Last active
June 27, 2023 07:47
-
-
Save jsoctocat/80fb0d9ecc096bcc656e5c4f60b6151e to your computer and use it in GitHub Desktop.
Auto Claim Twitch drop
This file contains hidden or 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 drop | |
// @version 1.00.01 | |
// @description Auto clicking "Click to claim a drop" under the chat | |
// @author jsoctocat | |
// @match https://www.twitch.tv/* | |
// @run-at document-idle | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitch.tv | |
// @grant none | |
// @namespace https://greasyfork.org/en/users/751816 | |
// ==/UserScript== | |
// credit: Auto Claim Twitch drop by bAdRocK | |
// credit: https://stackoverflow.com/questions/37098405/javascript-queryselector-find-div-by-innertext/37098508#37098508 | |
function findElementBasedOnText(selector, text) { | |
var elements = document.querySelectorAll(selector); | |
return Array.prototype.filter.call(elements, function(element){ | |
return RegExp(text).test(element.textContent); | |
}); | |
} | |
//let test0 = document.body.querySelectorAll('[data-test-selector="user-menu__toggle"]'); | |
//let test1 = document.body.querySelectorAll('[data-a-target="inventory-dropdown-link"]'); | |
//let test2 = document.body.querySelectorAll('[aria-label="Expand Player"]'); | |
var twitchDropOnMutate = function(mutationsList) { | |
mutationsList.forEach(mutation => { | |
//I dunno why this segment of code works better in this mutation but it works lol | |
if(loaded && document.querySelectorAll('[style="width: fit-content;"]')[0].children.length === 0 ) | |
{ | |
loaded = false; | |
//console.debug("left a stream"); | |
} | |
dropsRootContent = document.querySelectorAll(".drops-root__content"); | |
if(dropsRootContent.length > 0) twitchObserver.observe(dropsRootContent[0], {childList: true, subtree: true}); | |
if(loaded || document.querySelectorAll(".inventory-max-width").length > 0) | |
{ | |
let twitchChatDropClaimButton = findElementBasedOnText('button', 'Claim'); | |
if(twitchChatDropClaimButton.length > 0) twitchChatDropClaimButton[0].click(); | |
//console.debug("tried to claim"); | |
} | |
}) | |
} | |
var twitchObserver = new MutationObserver(twitchDropOnMutate); | |
let loaded = false; | |
var twitchPageOnMutate = function(mutationsList) { | |
mutationsList.forEach(mutation => { | |
if(!loaded) | |
{ | |
var twitchChatBox = document.querySelectorAll(".chat-input"); | |
if(twitchChatBox[0].children.length > 1) | |
{ | |
loaded = true; | |
twitchObserver.observe(twitchChatBox[0].children[0], {childList: true, subtree: true}); | |
//console.debug("found chat box"); | |
return; | |
} | |
} | |
}) | |
} | |
//childList includes only the immediate descendants | |
//subtree includes all descendants | |
//https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver/observe | |
//this is to check if root wrapper is changed, if user switched page to /drops/inventory | |
var twitchDropInventory = document.querySelectorAll(".root-scrollable__wrapper"); | |
twitchObserver.observe(twitchDropInventory[0], {childList: true}); | |
var dropsRootContent = document.querySelectorAll(".drops-root__content"); | |
if(dropsRootContent.length > 0) twitchObserver.observe(dropsRootContent[0], {childList: true, subtree: true}); | |
var twitchPageObserver = new MutationObserver(twitchPageOnMutate); | |
//var twitchChatBox = document.querySelectorAll('[style="width: fit-content;"]'); | |
twitchPageObserver.observe(document.querySelectorAll('[style="width: fit-content;"]')[0], {childList: true, subtree: true}); | |
console.debug("Auto Claim Twitch Drop Has Been Loaded!"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working as of 06/01/2023
need tampermonkey or similar to execute it as a userscript