Wait For Selector

Waits for selectors to match newly added nodes

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

  1. // ==UserScript==
  2. // @name Wait For Selector
  3. // @namespace http://tampermonkey.net/
  4. // @description Waits for elements
  5. // @author Kumirei
  6. // @include *community.wanikani.com*
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. ;(function (wfs) {
  11. let version = '1.0.1'
  12.  
  13. // Create new observer on body to monitor all DOM changes
  14. let observer = new MutationObserver(mutationHandler)
  15. observer.observe(document.getElementsByTagName('html')[0], { childList: true, subtree: true })
  16.  
  17. // Interface for interacting with the library
  18. let interface = {
  19. version,
  20. observer: observer,
  21. wait: waitForSelector,
  22. unwait: unwaitID,
  23. waits: {},
  24. waitsByID: {},
  25. nextID: 0,
  26. }
  27.  
  28. // Start
  29. installInterface()
  30.  
  31. // Creates a new entry to search for whenever a new element is added to the DOM
  32. function waitForSelector(selector, callback) {
  33. if (!interface.waits[selector]) interface.waits[selector] = {}
  34. interface.waits[selector][interface.nextID] = callback
  35. interface.waitsByID[interface.nextID] = selector
  36. search(selector, true)
  37. return interface.nextID++
  38. }
  39.  
  40. // Deletes a previously registered selector
  41. function unwaitID(ID) {
  42. delete interface.waits[interface.waitsByID[ID]][ID]
  43. delete interface.waitsByID[ID]
  44. }
  45.  
  46. // Makes sure that the public interface is the newest version and the same as the local one
  47. function installInterface() {
  48. if (!wfs) window.wfs = interface
  49. else if (wfs.version < interface.version) {
  50. wfs.version = interface.version
  51. wfs.observer.disconnect()
  52. wfs.observer = interface.observer
  53. wfs.wait = interface.wait
  54. wfs.unwait = interface.unwait
  55. }
  56. interface = wfs || interface
  57. }
  58.  
  59. // Waits until there has been more than 300 ms between mutations and then checks for new elements
  60. let lastMutationDate = 0 // Epoch of last mutation event
  61. let timeoutID = 0
  62. function mutationHandler(mutations) {
  63. let duration = Date.now() - lastMutationDate
  64. lastMutationDate = Date.now()
  65. if (duration < 300) {
  66. clearTimeout(timeoutID)
  67. timeoutID = setTimeout(() => {
  68. for (let selector in interface.waits) search(selector)
  69. }, 300)
  70. }
  71. }
  72.  
  73. // Searches for the selector and calls the callback on the found elements
  74. function search(selector, all = false) {
  75. document.querySelectorAll(selector).forEach((e, i) => {
  76. let callbacks = Object.values(interface.waits[selector])
  77. if (all || !e.WFSFound || e.WFSFound == lastMutationDate) {
  78. for (let callback of callbacks) callback(e)
  79. e.WFSFound = lastMutationDate
  80. }
  81. })
  82. }
  83. })(window.wfs)

QingJ © 2025

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