javascript - chrome扩展InjectDetails中文档的含义

标签 javascript google-chrome-extension document

我在 Chrome 扩展代码中使用 InjectDetails 对象。我对 runAt 字段中引用的文档有点困惑。

这是来自documentation的片段

runAt ( optional enum of "document_start", "document_end", or "document_idle" )

这里引用了哪个文档?可能的选项是:-

  • 原始文档(浏览器中加载的父框架)。
  • 将要注入(inject) JS/Css 的文档。 (原始文档中可能有多个iframe)
  • 文档在后台运行为background.html

我应该如何验证这一点?

最佳答案

如果allFramestrue,则“document”仅指顶级文档。
否则,它引用每个匹配框架的文档(例如顶级、iframe)。

您可以在 Content scripts 找到有关 run_at 的更多信息。文档:

run_at    string

Optional. Controls when the files in js are injected. Can be "document_start", "document_end", or "document_idle". Defaults to "document_idle".

In the case of "document_start", the files are injected after any files from css, but before any other DOM is constructed or any other script is run.

In the case of "document_end", the files are injected immediately after the DOM is complete, but before subresources like images and frames have loaded.

In the case of "document_idle", the browser chooses a time to inject scripts between "document_end" and immediately after the window.onload event fires. The exact moment of injection depends on how complex the document is and how long it is taking to load, and is optimized for page load speed.

Note: With "document_idle", content scripts may not necessarily receive the window.onload event, because they may run after it has already fired. In most cases, listening for the onload event is unnecessary for content scripts running at "document_idle" because they are guaranteed to run after the DOM is complete. If your script definitely needs to run after window.onload, you can check if onload has already fired by using the document.readyState property.

PS。属性名称采用驼峰命名法 chrome.tabs.executeScript (runAt, allFrames),但在 list 文件中拼写为下划线 (run_at, all_frames)。

关于javascript - chrome扩展InjectDetails中文档的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16647799/

相关文章:

html - 什么是DOM? (总结和重要性)

java - 如何将jsoup文档保存为文本文件

javascript - react native 谷歌登录失败。错误代码 10

javascript - 如何在 JetBrains Webstorm/PHPStorm 中编写 oop javascript(带有继承)以获得自动完成/智能感知

javascript - 在用户与窗口交互之前,Tampermonkey 脚本点击事件什么都不做

javascript - 如何编写一个针对以 "window.open()"打开的窗口的 Chrome 扩展程序?

google-chrome - 关闭 chrome 时清除本地存储(不是关闭特定选项卡时)?

javascript - jQuery:如果 div 不存在则添加它?

javascript - Chrome 扩展程序 : webNavigation while limiting permissions scope

javascript - ReactDOM.render 是否在渲染之前等待文档加载?