Torn Utils Library

Shared utility functions for Torn userscripts

Цей скрипт не слід встановлювати безпосередньо. Це - бібліотека для інших скриптів для включення в мета директиву // @require https://update.greasyfork.org/scripts/569328/1772270/Torn%20Utils%20Library.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(У мене вже є менеджер скриптів, дайте мені встановити його!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==UserScript==
// @name         Torn Utils Library
// @namespace    https://www.torn.com/
// @version      1.0
// @description  Shared utility functions for Torn userscripts
// @author       PFangy
// @grant        none
// @license      MIT
// ==/UserScript==

(function () {
  'use strict';

  // Create global namespace if it doesn't exist
  window.TornUtils = window.TornUtils || {};

  /**
   * Wait for a DOM element to appear
   * Useful for SPA / React pages like Torn
   *
   * @param {string} selector
   * @param {number} timeout
   * @returns {Promise<Element>}
   */
  window.TornUtils.waitForElement = function (selector, timeout = 10000) {
    return new Promise((resolve, reject) => {

      const element = document.querySelector(selector);
      if (element) {
        resolve(element);
        return;
      }

      const observer = new MutationObserver(() => {
        const el = document.querySelector(selector);
        if (el) {
          observer.disconnect();
          resolve(el);
        }
      });

      observer.observe(document.body, {
        childList: true,
        subtree: true
      });

      if (timeout) {
        setTimeout(() => {
          observer.disconnect();
          reject(new Error(`waitForElement timeout: ${selector}`));
        }, timeout);
      }
    });
  };

  /**
   * Wait for multiple elements
   */
  window.TornUtils.waitForElements = function (selector, timeout = 10000) {
    return new Promise((resolve, reject) => {

      const elements = document.querySelectorAll(selector);
      if (elements.length) {
        resolve(elements);
        return;
      }

      const observer = new MutationObserver(() => {
        const els = document.querySelectorAll(selector);
        if (els.length) {
          observer.disconnect();
          resolve(els);
        }
      });

      observer.observe(document.body, {
        childList: true,
        subtree: true
      });

      if (timeout) {
        setTimeout(() => {
          observer.disconnect();
          reject(new Error(`waitForElements timeout: ${selector}`));
        }, timeout);
      }
    });
  };

  /**
   * Observe element changes
   */
  window.TornUtils.observe = function (target, callback, options = { childList: true, subtree: true }) {
    const observer = new MutationObserver(callback);
    observer.observe(target, options);
    return observer;
  };

  /**
   * Wait until DOM ready
   */
  window.TornUtils.domReady = function () {
    return new Promise(resolve => {
      if (document.readyState === 'complete' || document.readyState === 'interactive') {
        resolve();
      } else {
        document.addEventListener('DOMContentLoaded', resolve);
      }
    });
  };

})();