我正在编写一个简单的页面计数器扩展名。如果页面匹配给定的表达式(在这种情况下是facebook.com),我希望它增加数组中的计数器(我希望能够单独计算单独的选项卡)。我设置了一个onCommitted事件来捕获经过的页面。
这里的其他人在每帧一次的onCommitted射击时遇到了麻烦,所以我确保包括帧检查。实际上,似乎有 nothing 我可以检查以确定哪个onCommitted事件是重要事件。我打印了每个请求的详细信息,两个请求都有相同的详细信息,直到时间戳。
它与附加一个onCommitted监听器有关,因为注释掉另一个监听器将“解决”问题。但我应该能够附加多个onCommitted监听器,对吗?
background.js
'use strict';
var trackedTabs = {};
//if this line is commented out, the below script runs correctly (increments once). otherwise increments twice
chrome.webNavigation.onCommitted.addListener(function(details) {console.log("listens to all, details are %o", details);});
var tabFunctions = {
'onBeforeRequest': function(details) {
//if a tab appears that matches the expression below, "register" it to trackedTabs
trackedTabs[details.tabId] = 0;
},
'onCommitted': function(details) {
if(details.frameId == 0) {
//this happens twice per page if the above listener is attached
console.log("Incrementing page count from %o", trackedTabs[details.tabId]);
trackedTabs[details.tabId]++;
//note that the request that increments from 0 to 1 and the one that increments from 1 to 2 have the same details
console.log(details);
}
}
};
chrome.webRequest.onBeforeRequest.addListener(tabFunctions['onBeforeRequest'], {'urls': ["*://facebook.com/", "*://*.facebook.com/*"], 'types': ['main_frame']}, ['blocking', 'requestBody']);
chrome.webNavigation.onCommitted.addListener(tabFunctions['onCommitted'], {'url': [{urlMatches: ".*?facebook.*?"}]});
的manifest.json
{
"manifest_version": 2,
"name": "__MSG_extension_name__",
"version": "3.2.0",
"description": "__MSG_extension_description__",
"author": "Lucidea",
"background": {
"scripts": ["background.js"]
},
"permissions": [
"<all_urls>",
"notifications",
"tabs",
"webRequest",
"webRequestBlocking",
"webNavigation",
"storage",
"unlimitedStorage",
"contextMenus"
]
}
日志输出中断
background.js:6 listens to all, details are Object
background.js:6 listens to all, details are Object
background.js:16 Incrementing page count from 0
background.js:19 Object {frameId: 0, processId: 897, tabId: 750, timeStamp: 1470333847318.882, transitionQualifiers: Array[1]…}
background.js:6 listens to all, details are Object
background.js:16 Incrementing page count from 1
background.js:19 Object {frameId: 0, processId: 897, tabId: 750, timeStamp: 1470333847318.882, transitionQualifiers: Array[1]…}
background.js:6 listens to all, details are Object
background.js:6 listens to all, details are Object
我已将该扩展名作为zip文件上传到https://www.dropbox.com/s/tbq6iqcgjojhkmo/oncommitted-runs-twice.zip
我的Chrome版本为52.0.2743.82