Wait For Selector

Waits for elements

目前為 2021-09-27 提交的版本,檢視 最新版本

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

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

QingJ © 2025

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