USToolkit

simple toolkit to help me

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/526417/1540623/USToolkit.js

  1. // ==UserScript==
  2. // @name USToolkit
  3. // @namespace https://gf.qytechs.cn/pt-BR/users/821661
  4. // @version 0.0.3
  5. // @run-at document-start
  6. // @author hdyzen
  7. // @description simple toolkit to help me
  8. // @license MIT
  9. // ==/UserScript==
  10.  
  11. /**
  12. * Asynchronously queries the DOM for an element matching the given selector.
  13. * If the element is not found immediately, it will observe the DOM for changes
  14. * and resolve once the element is found or reject after a timeout.
  15. *
  16. * @param {string} selector - The CSS selector to query for.
  17. * @param {number} [timeoutSeconds=10] - The maximum time to wait for the element, in seconds.
  18. * @returns {Promise<Element>} A promise that resolves with the found element or rejects with a timeout error.
  19. */
  20. function asyncQuerySelector(selector, timeoutSeconds = 10) {
  21. return new Promise((resolve, reject) => {
  22. const element = document.querySelector(selector);
  23. if (element) {
  24. resolve(element);
  25. }
  26.  
  27. const mutationsHandler = () => {
  28. const target = document.querySelector(selector);
  29. if (target) {
  30. observer.disconnect();
  31. resolve(target);
  32. }
  33. };
  34.  
  35. const observer = new MutationObserver(mutationsHandler);
  36.  
  37. observer.observe(document.body || document.documentElement || document, { childList: true, subtree: true });
  38.  
  39. setTimeout(() => {
  40. observer.disconnect();
  41. reject("Timeout 10 seconds");
  42. }, timeoutSeconds * 1000);
  43. });
  44. }

QingJ © 2025

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