Steam Retry

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

当前为 2023-09-06 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Steam Retry
  3. // @author https://github.com/Matt-RJ
  4. // @namespace https://github.com/Matt-RJ/tampermonkey-scripts/blob/master/steam-retry
  5. // @version 1.0.0
  6. // @description Automatically refreshes the page when Steam fails to load your inventory history.
  7. // @match *://steamcommunity.com/id/*/tradehistory*
  8. // @grant GM_getValue
  9. // @grant GM_setValue
  10. // ==/UserScript==
  11.  
  12. (function () {
  13. 'use strict';
  14.  
  15. // Max number of retries before the script stops. Set to 0 to retry indefinitely.
  16. const MAX_RETRIES = 5;
  17. // How long in ms to wait before refreshing the page when loading fails.
  18. const RETRY_DELAY = 500; // ! Beware of potential flashing lights and rate limits if lowering this
  19.  
  20. class SteamRetry {
  21. constructor({ maxRetries, retryDelay }) {
  22. if (!Number.isInteger(maxRetries)) {
  23. throw new Error('SteamRetry | maxRetries must be an integer.');
  24. }
  25. if (!Number.isInteger(retryDelay)) {
  26. throw new Error('SteamRetry | retryDelay must be an integer.');
  27. }
  28. this.ERROR_EL_CLASS_NAME = '.profile_fatalerror_message';
  29. this.LOAD_ERROR_MESSAGE = 'There was a problem loading your inventory history.';
  30. this.RETRY_KEY = 'steam-retry-retries-count';
  31. this.maxRetries = maxRetries;
  32. this.retryDelay = retryDelay;
  33. this.retries = 0;
  34. }
  35.  
  36. shouldRetry() {
  37. if (this.maxRetries <= 0) {
  38. return true;
  39. }
  40. return this.retries < this.maxRetries;
  41. }
  42.  
  43. async start() {
  44. // Once you run out of retries, the page stops refreshing automatically.
  45. // This resets the retry count when you refresh the page afterwards manually, or navigate away to a new page.
  46. window.addEventListener('beforeunload', async (e) => {
  47. e.preventDefault(); // Stop the event until the async function is done.
  48. // reset the retry count only if the maximum number of retries has been reached
  49. if (!this.shouldRetry()) {
  50. await GM_setValue(this.RETRY_KEY, 0);
  51. }
  52. return e;
  53. });
  54.  
  55. window.addEventListener('load', async () => {
  56. this.retries = await GM_getValue(this.RETRY_KEY, 0);
  57. console.log('SteamRetry | Starting...');
  58. console.log(`SteamRetry | Retry ${this.retries}/${this.maxRetries <= 0 ? 'ထ' : this.maxRetries}`);
  59. const errorEl = document.querySelector(this.ERROR_EL_CLASS_NAME);
  60. if (errorEl && errorEl.innerText === this.LOAD_ERROR_MESSAGE) {
  61. console.log('SteamRetry | Failed to load.');
  62. if (!this.shouldRetry()) {
  63. console.log('SteamRetry | Max retries reached.');
  64. return;
  65. }
  66. await this.refresh();
  67. } else {
  68. console.log('SteamRetry | Could not find error message; stopping.');
  69. await this.reset();
  70. }
  71. });
  72. }
  73.  
  74. async refresh() {
  75. console.log('SteamRetry | Refreshing...');
  76. await Promise.all([
  77. new Promise((resolve) => setTimeout(resolve, this.retryDelay)),
  78. GM_setValue(this.RETRY_KEY, this.retries + 1)
  79. ]);
  80. document.location.reload();
  81. }
  82.  
  83. async reset() {
  84. console.log('SteamRetry | Resetting retries to default...');
  85. await GM_setValue(this.RETRY_KEY, 0);
  86. }
  87. }
  88.  
  89. const steamRetry = new SteamRetry({
  90. maxRetries: MAX_RETRIES,
  91. retryDelay: RETRY_DELAY,
  92. });
  93. steamRetry.start();
  94. })();