Shared utility functions for Torn userscripts
Αυτός ο κώδικας δεν πρέπει να εγκατασταθεί άμεσα. Είναι μια βιβλιοθήκη για άλλους κώδικες που περιλαμβάνεται μέσω της οδηγίας meta // @require https://update.greasyfork.org/scripts/569328/1772270/Torn%20Utils%20Library.js
Torn Utils Library - Additional Info
Torn Utils Library is a lightweight collection of helper functions designed for userscripts running on modern single-page applications (SPA), such as Torn.
Many pages dynamically load and update their interface without performing a full page refresh. Scripts that rely on traditional page load events can fail when elements have not yet been rendered.
This library provides utilities that make it easier to:
All utilities are exposed under a single namespace: TornUtils. This prevents conflicts with other scripts and avoids polluting the global scope.
Include this library in your userscript using @require in the script header:
// @require https://update.greasyfork.org/scripts/569328/1772269/Torn%20Utils%20Library.js
Once included, access the utilities through TornUtils.
Waits until a DOM element matching the selector appears.
| Parameter | Type | Description |
|---|---|---|
| selector | string | CSS selector of the element to wait for |
| timeout | number | Maximum wait time in milliseconds (default: 10000) |
Returns a Promise resolving with the element.
await TornUtils.waitForElement('#profile-container');
const element = document.querySelector('#profile-container');
console.log(element);
Waits until one or more elements matching the selector exist.
| Parameter | Type | Description |
|---|---|---|
| selector | string | CSS selector of the elements |
| timeout | number | Maximum wait time in milliseconds (default: 10000) |
Returns a Promise resolving with a NodeList.
const items = await TornUtils.waitForElements('.item-row');
items.forEach(el => console.log(el.textContent));
Creates a MutationObserver for a DOM element.
| Parameter | Type | Description |
|---|---|---|
| target | Element | DOM node to observe |
| callback | function | Function executed on DOM mutations |
| options | object | MutationObserver config (default: { childList: true, subtree: true }) |
Returns the created MutationObserver instance.
const container = document.querySelector('#content');
const observer = TornUtils.observe(container, mutations => console.log('DOM changed'));
// Stop observing
observer.disconnect();
Returns a promise that resolves when the DOM is fully loaded and ready for interaction.
await TornUtils.domReady();
console.log("DOM is ready");
Dynamic websites often render content asynchronously. Using these helpers makes scripts:
It avoids repeated setTimeout loops or fragile DOM checks, providing a structured approach to detecting elements.
All functions are attached to window.TornUtils.
TornUtils.waitForElement(...);
TornUtils.observe(...);
TornUtils.domReady(...);
This library is designed for modern browsers that support: