我正在为扩展程序进行测试。我希望扩展程序在发出任何Web请求时更新徽章,但是徽章只有在我打开弹出窗口后才开始更新。有人可以解释为什么吗?
manifest.json
{
"manifest_version": 2,
"name": "Ad-scan",
"version": "0.1",
"content_scripts": [
{
"js": ["inject.js"],
"matches": ["<all_urls>"],
"run_at": "document_start",
"all_frames": true
}
],
"permissions": ["activeTab","declarativeContent","webRequest","webNavigation"],
"browser_action": {
"default_popup": "popup.html"
},
"background": {
"scripts": ["background.js"],
"persistent": true
}
}
background.js
chrome.webRequest.onBeforeRequest.addListener(function(details) {
chrome.tabs.query({active: true, currentWindow: true}, function(tabs){
chrome.tabs.sendMessage(tabs[0].id, {toInjected: details.url}, function(response){
chrome.runtime.sendMessage({webReqUpd: response.totalAds}) //ads are sent to popup
chrome.browserAction.setBadgeText({text: response.totalAds.toString()}) //badge is updated
})
})},{urls: ["<all_urls>"]});
inject.js
var ads = 0;
var adURLs = [];
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse){
ads = ads + 1 //ads value is updated
adURLs.push(request.toInjected) //array is updated
console.log(adURLs) //webRequests made on page are logged to console
//logging webRequests array to console to test webRequest event
sendResponse({totalAds: ads})//sending all ads to background
})
答案 0 :(得分:0)
我忽略了必须在webRequest API的权限中指定主机网络。 我在清单中添加了https和http的权限,现在可以使用了。
"permissions": ["activeTab","declarativeContent","webRequest","https://*/*","http://*/*"]