我正在尝试在我的后台页面中设置一个侦听器,当标签在其 URL 中使用“nytimes.com”更新时会增加一个计数器(最终我希望它在使用“NYTimes.com”更新标签时增加在它的标题中,但首先是第一件事)。我的代码如下:
// storage for counts
if (localStorage.getItem('nyt') == undefined || localStorage.getItem('nyt') == NaN)
{
localStorage.setItem('nyt', 0);
}
// turn the value from localStorage into an incrementable int
var nytCount = parseInt(localStorage.getItem('nyt'));
// listen for changed tabs and check URL
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
debugger;
var url = changeInfo.url;
if (changeInfo.status == 'complete' && url && url.indexOf("nytimes.com") !== -1) {
localStorage.setItem('nyt', ++nytCount);
}
});
后台页面在 manifest.json 中声明如下:
...
"background": {
"scripts": ["eventPage.js"],
"persistent": false
},
...
我也尝试过将“持久”声明为真的,但这似乎并没有奏效。谢谢你的帮助。