Steam Retry

Automatically refreshes the page when Steam fails to load your inventory history.

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

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

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Steam Retry
// @author       https://github.com/Matt-RJ
// @namespace    https://github.com/Matt-RJ/tampermonkey-scripts/blob/master/steam-retry
// @version      1.0.0
// @description  Automatically refreshes the page when Steam fails to load your inventory history.
// @match        *://steamcommunity.com/id/*/tradehistory*
// @grant        GM_getValue
// @grant        GM_setValue
// ==/UserScript==

(function () {
  'use strict';

  // Max number of retries before the script stops. Set to 0 to retry indefinitely.
  const MAX_RETRIES = 5;
  // How long in ms to wait before refreshing the page when loading fails.
  const RETRY_DELAY = 500; // ! Beware of potential flashing lights and rate limits if lowering this

  class SteamRetry {
    constructor({ maxRetries, retryDelay }) {
      if (!Number.isInteger(maxRetries)) {
        throw new Error('SteamRetry | maxRetries must be an integer.');
      }
      if (!Number.isInteger(retryDelay)) {
        throw new Error('SteamRetry | retryDelay must be an integer.');
      }
      this.ERROR_EL_CLASS_NAME = '.profile_fatalerror_message';
      this.LOAD_ERROR_MESSAGE = 'There was a problem loading your inventory history.';
      this.RETRY_KEY = 'steam-retry-retries-count';
      this.maxRetries = maxRetries;
      this.retryDelay = retryDelay;
      this.retries = 0;
    }

    shouldRetry() {
      if (this.maxRetries <= 0) {
        return true;
      }
      return this.retries < this.maxRetries;
    }

    async start() {
      // Once you run out of retries, the page stops refreshing automatically.
      // This resets the retry count when you refresh the page afterwards manually, or navigate away to a new page. 
      window.addEventListener('beforeunload', async (e) => {
        e.preventDefault(); // Stop the event until the async function is done.
        // reset the retry count only if the maximum number of retries has been reached
        if (!this.shouldRetry()) {
          await GM_setValue(this.RETRY_KEY, 0);
        }
        return e;
      });

      window.addEventListener('load', async () => {
        this.retries = await GM_getValue(this.RETRY_KEY, 0);
        console.log('SteamRetry | Starting...');
        console.log(`SteamRetry | Retry ${this.retries}/${this.maxRetries <= 0 ? 'ထ' : this.maxRetries}`);
        const errorEl = document.querySelector(this.ERROR_EL_CLASS_NAME);
        if (errorEl && errorEl.innerText === this.LOAD_ERROR_MESSAGE) {
          console.log('SteamRetry | Failed to load.');
          if (!this.shouldRetry()) {
            console.log('SteamRetry | Max retries reached.');
            return;
          }
          await this.refresh();
        } else {
          console.log('SteamRetry | Could not find error message; stopping.');
          await this.reset();
        }
      });
    }

    async refresh() {
      console.log('SteamRetry | Refreshing...');
      await Promise.all([
        new Promise((resolve) => setTimeout(resolve, this.retryDelay)),
        GM_setValue(this.RETRY_KEY, this.retries + 1)
      ]);
      document.location.reload();
    }

    async reset() {
      console.log('SteamRetry | Resetting retries to default...');
      await GM_setValue(this.RETRY_KEY, 0);
    }
  }

  const steamRetry = new SteamRetry({
    maxRetries: MAX_RETRIES,
    retryDelay: RETRY_DELAY,
  });
  steamRetry.start();
})();