在Chrome扩展程序的网页加载事件中 [英] On page load event in Chrome extensions
问题描述
我想检查chrome浏览器页面内容中的某些值,当它完全加载
时,就像
if(document.body.innerText.indexOf(Cat)!= - 1)
Where和我什么时候可以做我的支票?请给我一个清晰的例子
我读了一些关于Background.html和内容脚本的内容,但是我不能这样做 解决方案
在清单文件中注册一个内容脚本 run_at:document_idle
(这是默认值)并将代码放入内容脚本文件中。然后脚本将在页面准备就绪时运行。
如果您想从后台页面检测页面是否完全加载,请使用 chrome.webNavigation.onCompleted
事件并执行任何操作你想要的,比如调用 chrome.tabs.executeScript
来执行内容脚本。如果URL列表是动态的,或者无法使用 match pattern 语法。
$ b
chrome.webNavigation.onCompleted.addListener(function(详情){
chrome.tabs.executeScript(details.tabId,{
code:'if(document.body.innerText.indexOf(Cat)!= - 1){'+
'alert(Cat not found!);'+
'}'
});
},{
url:[{
//运行在example.com,example.net上,还有example.foo.com
hostContains:'.example。'
}],
});
manifest.json
中设置,例如:
{
name:Test,
version:1.0,
background:{scripts:[background.js]},
permissions:[webNavigation,*:// * / *],
manifest_version:2
}
I want to check some values in the content of chrome browser page when it completely loaded like that
if(document.body.innerText.indexOf("Cat") !=-1)
Where and when can I make my check? please give me an clear example I read some thing about "Background.html" and "Content script" but I can't do
Register a content script in the manifest file at "run_at": "document_idle"
(which is the default) and put your code in the content script file. Then the script will be run when the page is ready.
If you want to detect from the background page whether a page is completely loaded, use the chrome.webNavigation.onCompleted
event and do whatever you want, such as calling chrome.tabs.executeScript
to execute a content script. This method could be useful over the previous method if the list of URLs is dynamic or if the URL patterns cannot be described using the match pattern syntax.
chrome.webNavigation.onCompleted.addListener(function(details) {
chrome.tabs.executeScript(details.tabId, {
code: ' if (document.body.innerText.indexOf("Cat") !=-1) {' +
' alert("Cat not found!");' +
' }'
});
}, {
url: [{
// Runs on example.com, example.net, but also example.foo.com
hostContains: '.example.'
}],
});
The webNavigation
and host permissions have to be set in manifest.json
, e.g.:
{
"name": "Test",
"version": "1.0",
"background": { "scripts": ["background.js"] },
"permissions": [ "webNavigation", "*://*/*" ],
"manifest_version": 2
}
这篇关于在Chrome扩展程序的网页加载事件中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!