Greasy Fork is available in English.

autorefresh twitch on error 2000

a simple script to autorefresh twitch on error 2000

// ==UserScript==
// @name        autorefresh twitch on error 2000
// @namespace   Violentmonkey Scripts
// @include      *twitch.tv*
// @grant       none
// @version     2.8
// @author      KraXen72
// @description a simple script to autorefresh twitch on error 2000
// @icon        https://static.twitchcdn.net/assets/favicon-32-d6025c14e900565d6177.png
// ==/UserScript==
//initialize
console.log("initializing...");

//add locationchange custom event to detect if you navigated to a different twitch page, so the script can start again
/* These are the modifications: */
history.pushState = ( f => function pushState(){
    var ret = f.apply(this, arguments);
    window.dispatchEvent(new Event('pushstate'));
    window.dispatchEvent(new Event('locationchange'));
    return ret;
})(history.pushState);

history.replaceState = ( f => function replaceState(){
    var ret = f.apply(this, arguments);
    window.dispatchEvent(new Event('replacestate'));
    window.dispatchEvent(new Event('locationchange'));
    return ret;
})(history.replaceState);

window.addEventListener('popstate',()=>{
    window.dispatchEvent(new Event('locationchange'))
});



//defining of vars
var homeElems
var homeElem

var ErrorElems
var ErrorElem

var ErrorList
var playy

//on url change, restart the script
window.addEventListener('locationchange', function() {
  console.log("restarting script");
  checkError();
}, false);

//check for error 2000 and refresh
console.log("waiting for error 2000");

homeElems = document.getElementsByClassName("tw-animated-glitch-logo")
homeElem = homeElems[0]

if (typeof homeElem !== "undefined") {
  homeElem.style.background = 'rgb(23 60 23 / 1)' ;
}
checkError()

function checkError() {
  //the class which has error 2000 in it
  ErrorElems = document.getElementsByClassName("content-overlay-gate__allow-pointers")
  ErrorElem = ErrorElems[0]
  
  if (typeof ErrorElem !== "undefined" && ErrorElem.innerHTML == "There was a network error. Please try again. (Error #2000)") {
    console.log("reloading bc of error 2000")
    
    ErrorList = document.getElementsByClassName("content-overlay-gate__content")
    ErrorList[0].remove();

    playy = document.querySelector('[data-test-selector="player-overlay-play-button"]');
    playy.click();
    
    window.dispatchEvent(new Event('locationchange'))
  } else {
    setTimeout(function(){ checkError(); }, 500);
  }
}