0

我的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。

回答

1

chrome.tabs.query只应在您要立即查询标签状态时使用。

要监视您访问网站的频率,请使用chrome.tabs events之一,例如chrome.tabs.onUpdated。为避免重复计算,您应该检查changeInfo.status属性是否“完整”。

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) { 
    var url = changeInfo.url; // String or undefined 
    if (changeInfo.status == 'complete' && url && url.indexOf("nytimes.com") !== -1) { 
     // TODO: increase counter by one 
    } 
}); 

您的下一个问题是如何使用chrome.storage。它是一个异步API,因此您不能使用getter来读取实际值。此外,这些值在阅读后不会神奇地保存在存储器中。

对于储存柜台,我推荐localStorage超过chrome.storage。它是一个同步API,非常适合存储少量数据(如计数器)。只有字符串可以存储,所以要确保你投的价值为数字阅读后:

var nytCount = +localStorage.getItem('nyt') || 0; 

// Whenever you want to increase the counter: 
localStorage.setItem('nyt', ++nytCount); 

这假定只有一个页面与纽约时报变量相互作用。当使用变量(读/写)由多个页面(例如期权+背景页),你不能依赖于一个局部变量,并具有读取写入前的最新值:

localStorage.setItem('nyt', (+localStorage.getItem('nyt') || 0) + 1); 

如果你想采取异步路由(chrome.storage),您可以读取负载值(并推迟/排队chrome.tabs.onUpdated事件),或总是读+写上更新计数器:

chrome.storage.local.get('nyt', function(items) { 
    var nyt = items.nyt || 0; 
    chrome.storage.local.set({ 
     nyt: nyt + 1 
    }, function() { 
     // Only here, you can be certain that the value has been saved 
     // Usually less than one millisecond 
    }); 
}); 
+0

谢谢,罗布 - 你很有帮助! “+”是什么意思在“var nytCount = + localStorage.getItem('nyt')|| 0;”? – Zhankfor 2013-02-28 18:29:24

+1

@Zhankfor它“将”值转换为数字。 'localStorage.getItem('nyt')'可以返回'null'。 +0会返回0.它也可能包含一个无效的值,比如一个字符串。在这种情况下,一元'+'运算符会返回'NaN'。 '0'和'NaN'都是错误的,所以当OR运算符进入时,右边的表达式被计算出来,它是0.最终的结果是有效的字符串被转换为数字,无效的数字被替换为零。 – 2013-02-28 18:32:40

+0

再次感谢。我已经使用了你的代码,但是调试器似乎并不想在“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

1

那么我看到的主要问题是,chrome.storage.local.get调用是异步的,并有一个所需的回调。尝试将其更改为这样的事情:

var nytCount = 0; 
chrome.storage.local.get("nyt", function(items) { 
    doStuff(items.nyt); 
    // items.nyt is the value you want out of this 
}); 

function doStuff(nyt){ 
    nytCount = nyt; 
    if(nytCount == undefined) 
    nytCount = 0; 
    //put the rest of your code in here 
    //so that it all runs after you get the number out of storage 
} 

不要忘了更新你与chrome.storage.local.set调用存储具有的价值。对于那个回调是可选的。