UTILS_DOM Library

Eine nützliche Bibliothek für verschiedene Funktionen

Verze ze dne 01. 03. 2025. Zobrazit nejnovější verzi.

Tento skript by neměl být instalován přímo. Jedná se o knihovnu, kterou by měly jiné skripty využívat pomocí meta příkazu // @require https://update.greatest.deepsurf.us/scripts/528456/1545246/UTILS_DOM%20Library.js

  1. // ==UserScript==
  2. // @name UTILS_DOM Library
  3. // @namespace dannysaurus.epik
  4. // @version 1.0
  5. // @description Eine nützliche Bibliothek für verschiedene Funktionen
  6. //
  7. // @license MIT
  8. // @grant unsafeWindow
  9. // ==/UserScript==
  10.  
  11. /* jslint esversion: 11 */
  12. /* global unsafeWindow */
  13. (() => {
  14. 'use strict';
  15. unsafeWindow.dannysaurus_epik ||= {};
  16. unsafeWindow.dannysaurus_epik.libraries ||= {};
  17. unsafeWindow.dannysaurus_epik.libraries.UTILS_DOM = (() => {
  18.  
  19. /**
  20. * Tries to select an element by repeatedly attempting to find it.
  21. *
  22. * @param {Object} options - The options for selecting the element.
  23. * @param {String} [options.selectors] - The function to select the element.
  24. * @param {number} [options.maxAttempts=6] - The number of attempts to make.
  25. * @param {number} [options.intervalMs=10000] - The interval between attempts in milliseconds.
  26. * @returns {Promise<Element|NodeList>} The selected element(s).
  27. *
  28. * @throws {Error} If the element(s) could not be found.
  29. */
  30. const trySelectElement = async ({ selectors, maxAttempts = 6, intervalMs = 10000 } = {}) => {
  31. const sleep = () => new Promise(resolve => setTimeout(resolve, intervalMs));
  32.  
  33. for (let attemptCount = 0; attemptCount < maxAttempts; attemptCount++) {
  34. const elements = document.querySelector(selectors);
  35. if (elements instanceof Element || (elements instanceof NodeList && elements.length)) {
  36. return elements;
  37. }
  38. await sleep();
  39. }
  40. throw new Error('Element(s) not found');
  41. };
  42.  
  43. return {
  44. trySelectElement,
  45. };
  46. })();
  47. })();