waitForKeyElements gist port

A function that waits for a specific element to load before running a specified callback. portated from this GitHub gist: https://gist.github.com/BrockA/2625891#file-waitforkeyelements-js

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/476008/1255570/waitForKeyElements%20gist%20port.js

  1. // @require http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js
  2.  
  3. /*--- waitForKeyElements(): A utility function, for Greasemonkey scripts,
  4. that detects and handles AJAXed content.
  5.  
  6. Usage example:
  7.  
  8. waitForKeyElements (
  9. "div.comments"
  10. , commentCallbackFunction
  11. );
  12.  
  13. //--- Page-specific function to do what we want when the node is found.
  14. function commentCallbackFunction (jNode) {
  15. jNode.text ("This comment changed by waitForKeyElements().");
  16. }
  17.  
  18. IMPORTANT: This function requires your script to have loaded jQuery.
  19. */
  20. function waitForKeyElements (
  21. selectorTxt, /* Required: The jQuery selector string that
  22. specifies the desired element(s).
  23. */
  24. actionFunction, /* Required: The code to run when elements are
  25. found. It is passed a jNode to the matched
  26. element.
  27. */
  28. bWaitOnce, /* Optional: If false, will continue to scan for
  29. new elements even after the first match is
  30. found.
  31. */
  32. iframeSelector /* Optional: If set, identifies the iframe to
  33. search.
  34. */
  35. ) {
  36. var targetNodes, btargetsFound;
  37.  
  38. if (typeof iframeSelector == "undefined")
  39. targetNodes = $(selectorTxt);
  40. else
  41. targetNodes = $(iframeSelector).contents ()
  42. .find (selectorTxt);
  43.  
  44. if (targetNodes && targetNodes.length > 0) {
  45. btargetsFound = true;
  46. /*--- Found target node(s). Go through each and act if they
  47. are new.
  48. */
  49. targetNodes.each ( function () {
  50. var jThis = $(this);
  51. var alreadyFound = jThis.data ('alreadyFound') || false;
  52.  
  53. if (!alreadyFound) {
  54. //--- Call the payload function.
  55. var cancelFound = actionFunction (jThis);
  56. if (cancelFound)
  57. btargetsFound = false;
  58. else
  59. jThis.data ('alreadyFound', true);
  60. }
  61. } );
  62. }
  63. else {
  64. btargetsFound = false;
  65. }
  66.  
  67. //--- Get the timer-control variable for this selector.
  68. var controlObj = waitForKeyElements.controlObj || {};
  69. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  70. var timeControl = controlObj [controlKey];
  71.  
  72. //--- Now set or clear the timer as appropriate.
  73. if (btargetsFound && bWaitOnce && timeControl) {
  74. //--- The only condition where we need to clear the timer.
  75. clearInterval (timeControl);
  76. delete controlObj [controlKey]
  77. }
  78. else {
  79. //--- Set a timer, if needed.
  80. if ( ! timeControl) {
  81. timeControl = setInterval ( function () {
  82. waitForKeyElements ( selectorTxt,
  83. actionFunction,
  84. bWaitOnce,
  85. iframeSelector
  86. );
  87. },
  88. 300
  89. );
  90. controlObj [controlKey] = timeControl;
  91. }
  92. }
  93. waitForKeyElements.controlObj = controlObj;
  94. }