Skip to content

Instantly share code, notes, and snippets.

@mcprat
Last active December 11, 2021 17:07
Show Gist options
  • Save mcprat/df3b2c083b993cb66cbf4918a39a4d1b to your computer and use it in GitHub Desktop.
Save mcprat/df3b2c083b993cb66cbf4918a39a4d1b to your computer and use it in GitHub Desktop.
Twitch Auto Stream Live Checker
// ==UserScript==
// @name Twitch Live Checker Autobot
// @namespace https://gist.github.com/mpratt14/
// @version 2.1
// @description Automatically open a Twitch stream when they go live when watching someone else or offline
// @author DatPratt
// @match https://*.twitch.tv/*
// @icon https://www.google.com/s2/favicons?domain=twitch.tv
// @grant none
// ==/UserScript==
(function() {
'use strict';
const urls = [
"streamer1",
"streamer2",
"streamer3"
]
const root = "https://www.twitch.tv/";
var live, notlive, request, requests, url, previous
function clearChecker () {
live = []; notlive = []; requests = [];
previous ??= document.referrer;
}
function pickURL () {
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
for (let i = 0; i < requests.length; i++) {
url = urls[i];
request = requests[i];
if (!isLive()) {
if (live.length == 0 && window.location.href.includes(url)) {
console.log("TWITCH LIVE CHECKER:", "failed to check", url, "...aborting...");
break;
}
continue;
}
}
console.log("TWITCH LIVE CHECKER:", live, "are live");
console.log("TWITCH LIVE CHECKER:", notlive, "are NOT live");
console.log("TWITCH LIVE CHECKER:", "out of", urls);
//console.log("TWITCH LIVE CHECKER:", requests);
gotoURL();
clearChecker();
for (let i = 0; i < urls.length; i++) {
request = new XMLHttpRequest();
requests.push(request);
if (!getHTML(i)) {
break;
}
}
console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~",
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
function gotoURL () {
if (typeof live[0] !== "undefined") {
if (!window.location.href.includes(live[0]) &&
!window.location.href.includes("videos") &&
!window.location.href.includes("popout")) {
if (window.location.href.includes("referrer")) {
window.history.replaceState({}, document.title, window.location.href.split("?")[0].split("/")[3]);
} else if (previous.includes(live[0]) ||
previous.includes("referrer")) {
previous = window.location.href;
} else {
window.location.href = root + live[0];
}
} else {
console.log("TWITCH LIVE CHECKER:", "we are watching", window.location.href, "already");
}
}
}
function isLive () {
if (request.responseText.includes(url)) {
if (request.responseText.includes("isLiveBroadcast")) {
live.push(url);
} else {
notlive.push(url);
}
return true;
} else {
console.log("TWITCH LIVE CHECKER:", "failed to check", url, "...skipping...");
return false;
}
}
function getHTML (i) {
requests[i].open("GET", root + urls[i]);
//console.log("TWITCH LIVE CHECKER:", "fetching", root + urls[i]);
requests[i].onreadystatechange = function () {
if(this.readyState === XMLHttpRequest.DONE) {
var status = this.status;
if (status === 0 || (status >= 200 && status < 400)) {
//console.log("DONE", this.readyState, this.responseURL);
} else {
console.log("ERR:", this.readyState, this.responseURL);
}
}
}
try {
request.send();
return true;
} catch(err) {
console.log("TWITCH LIVE CHECKER:", "failed to fetch", urls[i], "...skipping...");
return false;
}
}
clearChecker();
var timer = setInterval(pickURL, 111111);
console.log("TWITCH LIVE CHECKER: USERSCRIPT STARTED");
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment