在我的 Google Chrome 扩展程序中,我有一个 Content Script (content.js) 和一个 Background Page (background.html)。我让 context.js 检查页面上出现的关键字。但是,我想等到页面完全加载后再搜索页面,因为关键字可能出现在页面底部。
参见内容页面操作三明治示例(文件),这基本上就是我正在做的事情。如果您加载扩展程序,您将看到该扩展程序仅在页面顶部出现“三明治”一词时才起作用。
尝试将其添加到manifest.json 文件的“content_scripts”部分。
"run_at": "document_end"
https://developer.chrome.com/docs/extensions/mv3/content_scripts/
作为清单
"run_at": "document_end"
的替代方案,可以应用标准的 javascript 事件处理方法。例如,DOMContentLoaded事件可用于运行需要加载 DOM 的逻辑。
manifest.json
"content_scripts": [
"js": ["content.js"],
"run_at": "document_start"
}
]
内容.js
console.log('The extension works');
// ... logic that does not need DOM
function run() {
console.log('The DOM is loaded');
// ... logic that needs DOM
}
document.addEventListener("DOMContentLoaded", run);