我在 Chrome 扩展代码中使用 InjectDetails
对象。我对 runAt
字段中引用的文档有点困惑。
这是来自documentation的片段
runAt ( optional enum of "document_start", "document_end", or "document_idle" )
这里引用了哪个文档?可能的选项是:-
- 原始文档(浏览器中加载的父框架)。
- 将要注入(inject) JS/Css 的文档。 (原始文档中可能有多个iframe)
- 文档在后台运行为background.html
我应该如何验证这一点?
最佳答案
如果allFrames
为true
,则“document”仅指顶级文档。
否则,它引用每个匹配框架的文档(例如顶级、iframe)。
您可以在 Content scripts 找到有关 run_at
的更多信息。文档:
PS。属性名称采用驼峰命名法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 theonload
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 afterwindow.onload
, you can check if onload has already fired by using thedocument.readyState
property.
chrome.tabs.executeScript
(runAt
, allFrames
),但在 list 文件中拼写为下划线 (run_at
, all_frames
)。
关于javascript - chrome扩展InjectDetails中文档的含义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16647799/