大家好,您可以看到我在Chrome扩展程序中还很新。
可以在DOM或页面完全加载之前和之后从content_scripts
运行脚本吗?
赞:
"content_scripts": [ {
"matches": ["<all_url>"],
"js": ["content.js"],
"all_frames": true,
"run_at": "document_start",
"run_at": "document_end"
} ]
或类似的东西
"content_scripts": [ {
"matches": ["<all_url>"],
"js": ["content1.js"],
"all_frames": true,
"run_at": "document_start"
} ],
"content_scripts": [ {
"matches": ["<all_url>"],
"js": ["content2.js"],
"all_frames": true,
"run_at": "document_end"
} ]
答案 0 :(得分:1)
您只能有一个content_scripts
条目,因此就像:
"content_scripts": [{
"matches": ["<all_url>"],
"js": ["content1.js"],
"all_frames": true,
"run_at": "document_start"
},{
"matches": ["<all_url>"],
"js": ["content2.js"],
"all_frames": true,
"run_at": "document_end"
}]
使用此设置,content1.js
将在开头运行,content2.js
在结尾运行。
答案 1 :(得分:1)
您正在朝正确的方向前进: 您可以使用run_at属性,但清单需要看起来像这样:
"content_scripts": [
{
"matches": ["<all_url>"],
"js": ["content1.js"],
"all_frames": true,
"run_at": "document_start"
},
{
"matches": ["<all_url>"],
"js": ["content2.js"],
"all_frames": true,
"run_at": "document_end"
}]
应该是定义所有内容脚本的对象数组,而不是两次声明content_script
属性。
$(document).ready()
:
const func1 = params => {
// Stuff that will happen as soon as it can
};
$(document).ready(() => {
// stuff that will happen when DOM is ready
});
如果您不使用jQuery,则可以通过快速的Google搜索来查找香草js中$(document).ready
的替代项。
chrome.tabs.executeScript()
docs从后台脚本以编程方式注入内容脚本。