Torn Utils Library

Shared utility functions for Torn userscripts

Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/569328/1772270/Torn%20Utils%20Library.js

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey 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.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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);
      }
    });
  };

})();