Reload on Twitch #2000 error

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

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 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エラーの監視を開始しました')
  }
  })();