javascript 在 Chrome 扩展内容脚本中,我是否必须在处理文档之前等待 document.ready?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5113318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:59:35  来源:igfitidea点击:

In a Chrome Extension content script, must I wait for document.ready before processing the document?

javascriptjquerygoogle-chromegoogle-chrome-extension

提问by David Mills

Specifically, I'm evaluating all of the images on a page to see if they have a certain attribute, and then adding some new <divs> to the DOM based on those attributes. Must I wait for document.ready to fire before performing these modifications in order to be guaranteed that Chrome has loaded all of the necessary pieces of the DOM?

具体来说,我正在评估页面上的所有图像以查看它们是否具有某个属性,然后根据这些属性将一些新的 <divs> 添加到 DOM。在执行这些修改之前,我是否必须等待 document.ready 触发才能保证 Chrome 已经加载了 DOM 的所有必要部分?

The problem I'm running into is that sometimes document.ready takes a short while to fire and the user is already browsing around the page, wondering why my extension hasn't yet had any effect. The problem usually only lasts a moment, but it's enough to be annoying.

我遇到的问题是有时 document.ready 需要很短的时间才能触发,而用户已经在浏览页面,想知道为什么我的扩展程序还没有产生任何效果。问题通常只持续片刻,但足以令人讨厌。

If I don't bother waiting for document.ready, and instead immediately process the document, everything seemsto work; but I wonder if I'm just getting lucky.

如果我不费心等待 document.ready,而是立即处理文档,一切似乎都正常;但我想知道我是否只是走运了。

回答by Mohamed Mansour

Actually, you don't have to wait. You can process right away in Content Scripts. Just make sure you don't use document_startin the run_atattribute.

实际上,您不必等待。您可以立即在Content Scripts 中进行处理。只要确保你不使用document_startrun_at属性。

In document_end, the files are injected immediately after the DOM is complete, but before subresources like images and frames have loaded. document_idle(the default value) happens even later.

在 中document_end,文件在 DOM 完成后立即注入,但在图像和帧等子资源加载之前。document_idle(默认值)发生得更晚。

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "matches": ["http://www.google.com/*"],
      "css": ["mystyles.css"],
      "js": ["jquery.js", "myscript.js"],
      "run_at": "document_end"
    }
  ],
  ...
}

回答by thinkdevcode

Short answer: yes.

简短的回答:是的。

Long answer: jQuery won't be able to grab any DOM elements that aren't already rendered. I know i've gotten into trouble a few times and it can be quite annoying to debug something for awhile and then realize I forgot to wrap the code in a document.ready. If its working for you, it's because your lucky. Also, you dont need to wrap it in a document.ready if your scripts are at the bottom of your page, just above your closing body tag.

长答案:jQuery 将无法获取任何尚未呈现的 DOM 元素。我知道我遇到了几次麻烦,调试一段时间后发现我忘记将代码包装在 document.ready 中可能会很烦人。如果它对你有用,那是因为你很幸运。此外,如果您的脚本位于页面底部,就在结束正文标记的上方,则无需将其包装在 document.ready 中。