waitUntilExists

waitUntilExists lib

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

  1. ;(function ($, window) {
  2.  
  3. var intervals = {};
  4. var removeListener = function(selector) {
  5.  
  6. if (intervals[selector]) {
  7. window.clearInterval(intervals[selector]);
  8. intervals[selector] = null;
  9. }
  10. };
  11. var found = 'waitUntilExists.found';
  12.  
  13. /**
  14. * @function
  15. * @property {object} jQuery plugin which runs handler function once specified
  16. * element is inserted into the DOM
  17. * @param {function|string} handler
  18. * A function to execute at the time when the element is inserted or
  19. * string "remove" to remove the listener from the given selector
  20. * @param {bool} shouldRunHandlerOnce
  21. * Optional: if true, handler is unbound after its first invocation
  22. * @example jQuery(selector).waitUntilExists(function);
  23. */
  24. $.fn.waitUntilExists = function(handler, shouldRunHandlerOnce, isChild) {
  25.  
  26. var selector = this.selector;
  27. var $this = $(selector);
  28. var $elements = $this.not(function() { return $(this).data(found); });
  29. if (handler === 'remove') {
  30. // Hijack and remove interval immediately if the code requests
  31. removeListener(selector);
  32. }
  33. else {
  34.  
  35. // Run the handler on all found elements and mark as found
  36. $elements.each(handler).data(found, true);
  37. if (shouldRunHandlerOnce && $this.length) {
  38. // Element was found, implying the handler already ran for all
  39. // matched elements
  40. removeListener(selector);
  41. }
  42. else if (!isChild) {
  43. // If this is a recurring search or if the target has not yet been
  44. // found, create an interval to continue searching for the target
  45. intervals[selector] = window.setInterval(function () {
  46. $this.waitUntilExists(handler, shouldRunHandlerOnce, true);
  47. }, 500);
  48. }
  49. }
  50. return $this;
  51. };
  52. }(jQuery, window));

QingJ © 2025

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