简体   繁体   English

在 Chrome 扩展 javascript 中循环导致页面无法加载

[英]Loop in chrome extension javascript is making the page not loading

i just wanted to make a condition before the script runs so i decided to run this loop before my code, however the website does not want to load with it:我只是想在脚本运行之前做一个条件,所以我决定在我的代码之前运行这个循环,但是网站不想加载它:

s =document.documentElement.innerText.split(",")[5]
while (s != " 2021 - June 25"){
    s =document.documentElement.innerText.split(",")[5]
}

it checks if it has correct string so it can run the full script.它检查它是否有正确的字符串,以便它可以运行完整的脚本。 I made this because the website has dynamic content and load in 2 times.我这样做是因为该网站具有动态内容并在 2 次内加载。 Hope you understand me.希望你能理解我。 website is:https://www.nike.com/events-registration/event?id=1618516799261网址是:https://www.nike.com/events-registration/event?id=1618516799261

thank you.谢谢你。

You can use the following technique:您可以使用以下技术:

  1. Set an interval to check for an image DOM element and add an event listener to it.设置时间间隔以检查图像 DOM 元素并向其添加事件侦听器。
  2. The event listener should wait for the image to load, and then you can perform your actions.事件侦听器应该等待图像加载,然后您可以执行您的操作。

We choose images because they load slow.我们选择图像是因为它们加载缓慢。 Try to find out the class/id(should not be dynamic) of any image.尝试找出任何图像的类/ID(不应是动态的)。

var checkInterval = setInterval(()=>{
if(document.querySelector('.slowImageClass')!=null){
document.querySelector('.slowImageClass').addEventListener('load', () => {
   clearInterval(checkInterval ); //To stop the loop
   //Your actions

});
}
},300  /**  time in milliseconds        **/);

Load event works on only a few DOM Element types. 加载事件仅适用于少数 DOM 元素类型。 Image is one of them.图片就是其中之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM