Wait for key element

A utility function, for Greasemonkey scripts, that detects and handles dynamic content.

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

  1. // ==UserScript==
  2. // @name Wait for key elements
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description A utility function, for Greasemonkey scripts, that detects and handles dynamic content.
  6. // @author https://github.com/cgriebel
  7. // @grant none
  8. // @include *
  9. // ==/UserScript==
  10.  
  11. /*--- waitForKeyElements():
  12. Usage example:
  13.  
  14. waitForKeyElements ("div.comments", commentCallbackFunction);
  15.  
  16. //--- Page-specific function to do what we want when the node is found.
  17. function commentCallbackFunction (jNode) {
  18. jNode.text ("This comment changed by waitForKeyElements().");
  19. }
  20. */
  21. function waitForKeyElements(
  22. selectorTxt, /* Required: The selector string that specifies the desired element(s). */
  23. actionFunction, /* Required: The code to run when elements are found. It is passed a jNode to the matched element. */
  24. waitOnce, /* Optional: If false, will continue to scan for new elements even after the first match is found. */
  25. iframeSelector /* Optional: If set, identifies the iframe to search. */
  26. ) {
  27. const dataAttr = 'data-wait-for-elements-found';
  28. var targetNodes, targetsFound;
  29.  
  30. if (typeof iframeSelector == "undefined") {
  31. var nodes = document.querySelectorAll(selectorTxt);
  32. targetNodes = nodes ? [...nodes] : null;
  33. }
  34. else {
  35. // TODO: CG - test iframes, I currently have no use for this
  36. // targetNodes = $(iframeSelector).contents()
  37. // .find(selectorTxt);
  38. }
  39.  
  40. if (targetNodes && targetNodes.length > 0) {
  41. targetsFound = true;
  42. /*--- Found target node(s). Go through each and act if they
  43. are new.
  44. */
  45. targetNodes.forEach((node) => {
  46. if (!node.getAttribute(dataAttr)) {
  47. //--- Call the payload function.
  48. var cancelFound = actionFunction(node);
  49. if (cancelFound) {
  50. targetsFound = false;
  51. }
  52. else {
  53. node.setAttribute(dataAttr, true);
  54. }
  55. }
  56. })
  57. }
  58. else {
  59. targetsFound = false;
  60. }
  61.  
  62. //--- Get the timer-control variable for this selector.
  63. var controlObj = waitForKeyElements.controlObj || {};
  64. var controlKey = selectorTxt.replace(/[^\w]/g, "_");
  65. var timeControl = controlObj[controlKey];
  66.  
  67. //--- Now set or clear the timer as appropriate.
  68. if (targetsFound && waitOnce && timeControl) {
  69. //--- The only condition where we need to clear the timer.
  70. clearInterval(timeControl);
  71. delete controlObj[controlKey]
  72. }
  73. else {
  74. //--- Set a timer, if needed.
  75. if (!timeControl) {
  76. timeControl = setInterval(function () {
  77. waitForKeyElements(selectorTxt,
  78. actionFunction,
  79. waitOnce,
  80. iframeSelector
  81. );
  82. },
  83. 300
  84. );
  85. controlObj[controlKey] = timeControl;
  86. }
  87. }
  88. waitForKeyElements.controlObj = controlObj;
  89. }

QingJ © 2025

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