Reload on Twitch #2000 error

Twitchでエラー(#2000)が出たときにページを再読み込みするスクリプトです

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name    Reload on Twitch #2000 error
// @namespace    ReloadonTwitch#2000error
// @version    0.1.1
// @description    Twitchでエラー(#2000)が出たときにページを再読み込みするスクリプトです
// @author    khsk modded by bob_puyon
// @match    https://www.twitch.tv/*
// @grant    none
// ==/UserScript==

(function() {
  'use strict';
  // まずはメイン画面のロード完了を待つ
  const mo = new MutationObserver((data1, data2) => {
    const videoPlayer = document.querySelector('.video-player__default-player')
    if (videoPlayer) {
      errorWatcher()
      mo.disconnect();
      return
    }
  })
  mo.observe(document.body, {
    childList: true, subtree: true
  });


  // ビデオ監視処理定義
  const errorWatcher = () => {
    const errorMessage = 'ネットワークエラーが発生しました。再度お試しください。(エラー #2000)'

    // 検知クリックによる変更検知無限ループを防ぐためのフラグ。筋が悪い後から追加の場当たり処理
    let isCallbackClicked = false

    const videoPlayer = document.querySelector('.video-player__default-player')
    if (!videoPlayer) {
      console.log('no player')
      return
    }

    const mo = new MutationObserver((data1, data2) => {
      const message = document.querySelector('.content-overlay-gate__allow-pointers.tw-c-text-overlay')
      if (!message) {
        isCallbackClicked = false
        return
      }

      if (message.textContent != errorMessage) {
        isCallbackClicked = false
        return
      }

      if (isCallbackClicked) {
        return
      }
      console.log('Twitchでの#2000エラーを検出しました ページを再読み込みします')
      isCallbackClicked = true
      location.reload();
    });

    const options = {childList: true, subtree:true};
    mo.observe(videoPlayer, options);
    console.log('Twitchでの#2000エラーの監視を開始しました')
  }
  })();