UTILS_DOM Library

dom manipulation library

目前为 2025-03-01 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/528456/1545251/UTILS_DOM%20Library.js

  1. // ==UserScript==
  2. // @name UTILS_DOM Library
  3. // @namespace dannysaurus.epik
  4. // @version 1.0
  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 } = {}) => {
  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. const getUsername = () => {
  44. return document.querySelector(CONFIG.selectors.usernameContainer)?.textContent || '';
  45. };
  46.  
  47. return {
  48. trySelectElement,
  49. getUsername,
  50. };
  51. })();
  52. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址