Wait Until Exists Version v0.2

A faster way than document ready

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

  1. /*
  2. * Wait Until Exists Version v0.2 - http://javascriptisawesome.blogspot.com/
  3. *
  4. *
  5. * TERMS OF USE - Wait Until Exists
  6. *
  7. * Open source under the BSD License.
  8. *
  9. * Copyright © 2011 Ivan Castellanos
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without modification,
  13. * are permitted provided that the following conditions are met:
  14. *
  15. * Redistributions of source code must retain the above copyright notice, this list of
  16. * conditions and the following disclaimer.
  17. * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * Neither the name of the author nor the names of contributors may be used to endorse
  22. * or promote products derived from this software without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
  25. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  26. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  27. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  28. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  29. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  30. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  31. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  32. * OF THE POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. (function(){
  36. var _waitUntilExists = {
  37. pending_functions : [],
  38. loop_and_call : function()
  39. {
  40. if(!_waitUntilExists.pending_functions.length){return}
  41. for(var i=0;i<_waitUntilExists.pending_functions.length;i++)
  42. {
  43. var obj = _waitUntilExists.pending_functions[i];
  44. var resolution = document.getElementById(obj.id);
  45. if(obj.id == document){
  46. resolution = document.body;
  47. }
  48. if(resolution){
  49. var _f = obj.f;
  50. _waitUntilExists.pending_functions.splice(i, 1)
  51. if(obj.c == "itself"){obj.c = resolution}
  52. _f.call(obj.c)
  53. i--
  54. }
  55. }
  56. },
  57. global_interval : setInterval(function(){_waitUntilExists.loop_and_call()},5)
  58. }
  59. if(document.addEventListener){
  60. document.addEventListener("DOMNodeInserted", _waitUntilExists.loop_and_call, false);
  61. clearInterval(_waitUntilExists.global_interval);
  62. }
  63. window.waitUntilExists = function(id,the_function,context){
  64. context = context || window
  65. if(typeof id == "function"){context = the_function;the_function = id;id=document}
  66. _waitUntilExists.pending_functions.push({f:the_function,id:id,c:context})
  67. }
  68. waitUntilExists.stop = function(id,f){
  69. for(var i=0;i<_waitUntilExists.pending_functions.length;i++){
  70. if(_waitUntilExists.pending_functions[i].id==id && (typeof f == "undefined" || _waitUntilExists.pending_functions[i].f == f))
  71. {
  72. _waitUntilExists.pending_functions.splice(i, 1)
  73. }
  74. }
  75. }
  76. waitUntilExists.stopAll = function(){
  77. _waitUntilExists.pending_functions = []
  78. }
  79. })

QingJ © 2025

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