我的chrome.tabs.query代码在调试器中查找我的扩展时似乎没有执行。我正在尝试使用chrome.storage API来记录访问nytimes.com文章的次数,并且由于我在开始时添加了chrome.storage代码,因此调试器似乎不会进入chrome.tabs.query功能。自chrome.storage加入后代码看起来没有执行
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。
谢谢,罗布 - 你很有帮助! “+”是什么意思在“var nytCount = + localStorage.getItem('nyt')|| 0;”? – Zhankfor 2013-02-28 18:29:24
@Zhankfor它“将”值转换为数字。 'localStorage.getItem('nyt')'可以返回'null'。 +0会返回0.它也可能包含一个无效的值,比如一个字符串。在这种情况下,一元'+'运算符会返回'NaN'。 '0'和'NaN'都是错误的,所以当OR运算符进入时,右边的表达式被计算出来,它是0.最终的结果是有效的字符串被转换为数字,无效的数字被替换为零。 – 2013-02-28 18:32:40
再次感谢。我已经使用了你的代码,但是调试器似乎并不想在“chrome.storage.local.get('nyt',函数(items)”)之后进入花括号。现在看起来像这样: 'var nytCount = + localStorage。getItem('nyt')|| 0; console.log(nytCount); chrome.tabs.onUpdated.addListener(功能(tabId,changeInfo,标签) { 变种URL = changeInfo.url; //字符串或未定义 如果(changeInfo.status == '完成' && URL &&网址。的indexOf( “NYTimes.com”)== -1){ localStorage.setItem( '纽约时报',++ nytCount);} !' (抱歉它是如此凌乱) – Zhankfor 2013-02-28 18:44:12