waitForKeyElements-CoeJoder-fork

A utility function for userscripts that detects and handles AJAXed content.

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/502635/1422102/waitForKeyElements-CoeJoder-fork.js

  1. // ==UserScript==
  2. // @version 1.3.0
  3. // @name waitForKeyElements.js (CoeJoder fork)
  4. // @description A utility function for userscripts that detects and handles AJAXed content.
  5. // @namespace https://github.com/CoeJoder/waitForKeyElements.js
  6. // @author CoeJoder
  7. // @homepage https://github.com/CoeJoder/waitForKeyElements.js
  8. // @source https://raw.githubusercontent.com/CoeJoder/waitForKeyElements.js/master/waitForKeyElements.js
  9. //
  10. // ==/UserScript==
  11.  
  12. /**
  13. * A utility function for userscripts that detects and handles AJAXed content.
  14. *
  15. * @example
  16. * waitForKeyElements("div.comments", (element) => {
  17. * element.innerHTML = "This text inserted by waitForKeyElements().";
  18. * });
  19. *
  20. * waitForKeyElements(() => {
  21. * const iframe = document.querySelector('iframe');
  22. * if (iframe) {
  23. * const iframeDoc = iframe.contentDocument || iframe.contentWindow.document;
  24. * return iframeDoc.querySelectorAll("div.comments");
  25. * }
  26. * return null;
  27. * }, callbackFunc);
  28. *
  29. * @param {(string|function)} selectorOrFunction - The selector string or function.
  30. * @param {function} callback - The callback function; takes a single DOM element as parameter.
  31. * If returns true, element will be processed again on subsequent iterations.
  32. * @param {boolean} [waitOnce=true] - Whether to stop after the first elements are found.
  33. * @param {number} [interval=300] - The time (ms) to wait between iterations.
  34. * @param {number} [maxIntervals=-1] - The max number of intervals to run (negative number for unlimited).
  35. */
  36. function waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals) {
  37. if (typeof waitOnce === "undefined") {
  38. waitOnce = true;
  39. }
  40. if (typeof interval === "undefined") {
  41. interval = 300;
  42. }
  43. if (typeof maxIntervals === "undefined") {
  44. maxIntervals = -1;
  45. }
  46. if (typeof waitForKeyElements.namespace === "undefined") {
  47. waitForKeyElements.namespace = Date.now().toString();
  48. }
  49. var targetNodes = (typeof selectorOrFunction === "function")
  50. ? selectorOrFunction()
  51. : document.querySelectorAll(selectorOrFunction);
  52.  
  53. var targetsFound = targetNodes && targetNodes.length > 0;
  54. if (targetsFound) {
  55. targetNodes.forEach(function(targetNode) {
  56. var attrAlreadyFound = `data-userscript-${waitForKeyElements.namespace}-alreadyFound`;
  57. var alreadyFound = targetNode.getAttribute(attrAlreadyFound) || false;
  58. if (!alreadyFound) {
  59. var cancelFound = callback(targetNode);
  60. if (cancelFound) {
  61. targetsFound = false;
  62. }
  63. else {
  64. targetNode.setAttribute(attrAlreadyFound, true);
  65. }
  66. }
  67. });
  68. }
  69.  
  70. if (maxIntervals !== 0 && !(targetsFound && waitOnce)) {
  71. maxIntervals -= 1;
  72. setTimeout(function() {
  73. waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals);
  74. }, interval);
  75. }
  76. }

QingJ © 2025

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