waitForKeyElements utility function

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

بۇ قوليازمىنى بىۋاسىتە قاچىلاشقا بولمايدۇ. بۇ باشقا قوليازمىلارنىڭ ئىشلىتىشى ئۈچۈن تەمىنلەنگەن ئامبار بولۇپ، ئىشلىتىش ئۈچۈن مېتا كۆرسەتمىسىگە قىستۇرىدىغان كود: // @require https://update.greatest.deepsurf.us/scripts/446257/1059316/waitForKeyElements%20utility%20function.js

  1. // ==UserScript==
  2. // @version 1.0.0
  3. // @name waitForKeyElements utility function
  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. * Usage example:
  16. *
  17. * function callback(domElement) {
  18. * domElement.innerHTML = "This text inserted by waitForKeyElements().";
  19. * }
  20. *
  21. * waitForKeyElements("div.comments", callback);
  22. * // or
  23. * waitForKeyElements(selectorFunction, callback);
  24. *
  25. * @param {(string|function)} selectorOrFunction - The selector string or function.
  26. * @param {function} callback - The callback function; takes a single DOM element as parameter.
  27. * If returns true, element will be processed again on subsequent iterations.
  28. * @param {boolean} [waitOnce=true] - Whether to stop after the first elements are found.
  29. * @param {number} [interval=300] - The time (ms) to wait between iterations.
  30. * @param {number} [maxIntervals=-1] - The max number of intervals to run (negative number for unlimited).
  31. */
  32. function waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals) {
  33. if (typeof waitOnce === "undefined") {
  34. waitOnce = true;
  35. }
  36. if (typeof interval === "undefined") {
  37. interval = 300;
  38. }
  39. if (typeof maxIntervals === "undefined") {
  40. maxIntervals = -1;
  41. }
  42. var targetNodes = (typeof selectorOrFunction === "function")
  43. ? selectorOrFunction()
  44. : document.querySelectorAll(selectorOrFunction);
  45.  
  46. var targetsFound = targetNodes && targetNodes.length > 0;
  47. if (targetsFound) {
  48. targetNodes.forEach(function(targetNode) {
  49. var attrAlreadyFound = "data-userscript-alreadyFound";
  50. var alreadyFound = targetNode.getAttribute(attrAlreadyFound) || false;
  51. if (!alreadyFound) {
  52. var cancelFound = callback(targetNode);
  53. if (cancelFound) {
  54. targetsFound = false;
  55. }
  56. else {
  57. targetNode.setAttribute(attrAlreadyFound, true);
  58. }
  59. }
  60. });
  61. }
  62.  
  63. if (maxIntervals !== 0 && !(targetsFound && waitOnce)) {
  64. maxIntervals -= 1;
  65. setTimeout(function() {
  66. waitForKeyElements(selectorOrFunction, callback, waitOnce, interval, maxIntervals);
  67. }, interval);
  68. }
  69. }