Greasy Fork is available in English.

UTILS_DOM Library

dom manipulation library

Mint 2025.03.02.. Lásd a legutóbbi verzió

Ezt a szkriptet nem ajánlott közvetlenül telepíteni. Ez egy könyvtár más szkriptek számára, amik tartalmazzák a // @require https://update.greatest.deepsurf.us/scripts/528456/1545557/UTILS_DOM%20Library.js hivatkozást.

  1. // ==UserScript==
  2. // @name UTILS_DOM Library
  3. // @namespace dannysaurus.epik
  4. // @version 1.1
  5. // @description dom manipulation library
  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, throwError = true, } = {}) => {
  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 with selectors: ${selectors}`);
  41. };
  42.  
  43. const getUsername = () => {
  44. return document.querySelector(CONFIG.selectors.usernameContainer)?.textContent || '';
  45. };
  46.  
  47. /**
  48. * Checks if the specified keys are pressed.
  49. *
  50. * @param {KeyboardEvent} event - The keyboard event.
  51. * @param {Object} keysToCheck - The keys to check.
  52. * @returns {boolean} True if all specified keys are pressed, false otherwise.
  53. */
  54. const areKeysPressed = (event, keysToCheck) => {
  55. const keys = Object.keys(keysToCheck || []);
  56. if (keys.length === 0) {
  57. return false;
  58. }
  59. return Object.keys(keysToCheck).every(key => event[key] === keysToCheck[key]);
  60. };
  61.  
  62. return {
  63. trySelectElement,
  64. getUsername,
  65. areKeysPressed,
  66. };
  67. })();
  68. })();