Library | querySelectorInterval

Use document.querySelector() to continuously look for an Element. Call a function when found. Powered by requestAnimationFrame.

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

  1. // ==UserScript==
  2. // @name Library: querySelectorInterval
  3. // @namespace org.sidneys.userscripts
  4. // @homepage https://gist.githubusercontent.com/sidneys/c6b91437cb89346281bb2f739c1d6ae2/raw/
  5. // @version 0.7.4
  6. // @description Use document.querySelector() to continuously look for an Element. Call a function when found. Powered by requestAnimationFrame.
  7. // @author sidneys
  8. // @icon https://i.imgur.com/nmbtzlX.png
  9. // @match *://*/*
  10. // ==/UserScript==
  11.  
  12.  
  13. /**
  14. * ESLint
  15. * @exports
  16. */
  17. /* exported querySelectorInterval, clearQuerySelectorInterval */
  18.  
  19.  
  20. /**
  21. * Unique identifier returned by querySelectorInterval().
  22. * @typedef {Number} intervalHandle
  23. */
  24.  
  25. /**
  26. * This callback is displayed as a global member.
  27. * @callback intervalCallback
  28. * @param {Element} Element found by document.querySelector()
  29. */
  30.  
  31. /**
  32. * Repeatedly try to find (using a optional time delay) the first element
  33. * that is a descendant of a base element, and matches the selectors.
  34. * Call a callback with the found element as argument when successful.
  35. * Returns an identifier for removal via clearQuerySelectorInterval().
  36. * @param {Element} element - Base Element
  37. * @param {String} selectors - DOMString containing one or more selectors to match against
  38. * @param {intervalCallback=} callback - The callback that is called when an Element was found
  39. * @param {Number=} delay - Duration (ms) the timer should wait between executions
  40. * @return {intervalHandle} - Unique interval handle
  41. * @global
  42. */
  43. let querySelectorInterval = (element, selectors, callback = () => {}, delay = 0) => {
  44. // console.debug('querySelectorInterval')
  45.  
  46. // requestAnimationFrame identifier
  47. let requestId
  48.  
  49. // Initial timestamp
  50. let lastTimestamp = new Date().getTime()
  51.  
  52. // Lookup Logic
  53. let lookup = () => {
  54. // Get current timestamp
  55. const currentTimestamp = new Date().getTime()
  56.  
  57. // Get elapsed time (between timestamps)
  58. const elapsedDuration = currentTimestamp - lastTimestamp
  59.  
  60. // Elapsed time larger than delay?
  61. if (elapsedDuration >= delay) {
  62. // Test if selectors finds a descendant beneath element
  63. const foundElement = element.querySelector(selectors)
  64.  
  65. // Element found
  66. if (foundElement) {
  67. callback(foundElement)
  68. }
  69.  
  70. // Update last timestamp
  71. lastTimestamp = new Date().getTime()
  72. }
  73.  
  74. // Rerun Lookup
  75. requestId = window.requestAnimationFrame(lookup)
  76. }
  77.  
  78. // Initial lookup
  79. requestId = window.requestAnimationFrame(lookup)
  80.  
  81. return requestId
  82. }
  83.  
  84. /**
  85. * Cancels intervals scheduled via querySelectorInterval().
  86. * @param {intervalHandle} handle - Unique interval handle
  87. * @global
  88. */
  89. let clearQuerySelectorInterval = (handle) => {
  90. // console.debug('clearQuerySelectorInterval')
  91.  
  92. // Cancel interval
  93. window.cancelAnimationFrame(handle)
  94. }

QingJ © 2025

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