在调试器中查看我的扩展时,我的 chrome.tabs.query 代码似乎没有执行。我正在尝试使用 chrome.storage API 来记录访问 nytimes.com 上的文章的次数,并且由于我在开头添加了 chrome.storage 代码,因此调试器似乎没有进入 chrome.tabs.query功能。
var nytCount = chrome.storage.local.get["nyt"];
// if nytCount doesn't exist in chrome local storage, set to 0
if (nytCount === undefined)
{
nytCount = 0;
}
/*
* Below is adapted from user Rob W at Stack Overflow (http://stackoverflow.com/questions/10413911/how-to-get-the-currently-opened-tabs-url-in-my-page-action-popup/10417327#10417327)
*
// Gets URL from currently active window (this should all be onload I suspect)*/
chrome.tabs.query({
// Select active tabs
active: true,
// In the current window
windowId: chrome.windows.WINDOW_ID_CURRENT
}, function(array_of_Tabs) {
// Since there can only be one active tab in one active window, the array has only one element
var tab = array_of_Tabs[0];
var title = tab.title;
if (title.indexOf("NYTimes.com") !== -1)
{
nytCount++;
}
// rest of if conditions for those sites that have identifiers in tab titles
});
alert(nytCount);
有任何想法吗?在我将 nytCount 初始化为 0 之前它工作得很好,但是当然它的值只能上升到 1,然后它会在下一次运行代码时重新初始化。