Debugging 添加chrome.storage后代码似乎未执行
在调试器中查看扩展名时,我的chrome.tabs.query代码似乎没有执行。我正在试验chrome.storage API来记录访问nytimes.com上文章的次数,由于我在开始时添加了chrome.storage代码,调试器似乎没有进入chrome.tabs.query函数Debugging 添加chrome.storage后代码似乎未执行,debugging,google-chrome,google-chrome-extension,local-storage,google-chrome-devtools,Debugging,Google Chrome,Google Chrome Extension,Local Storage,Google Chrome Devtools,在调试器中查看扩展名时,我的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 t
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。我看到的主要问题是,
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
调用更新存储中的值。对于该选项,回调是可选的。chrome.tabs.query
仅当您要立即查询选项卡的状态时才应使用
要监视您访问站点的频率,请使用其中一个,例如。为避免重复计算,应检查changeInfo.status
属性是否为“完成”
下一个问题是如何使用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);
这假设只有一个页面与nyt变量交互。当变量被多个页面(例如选项+背景页面)使用(读/写)时,您不能依赖于局部变量,必须在写入之前读取最新值:
localStorage.setItem('nyt', (+localStorage.getItem('nyt') || 0) + 1);
如果要采用异步路由(),可以在加载时读取值(并延迟/排队chrome.tabs.onUpdate
事件),或者在更新时始终读取并写入计数器:
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
});
});
谢谢你,罗伯-你帮了大忙!“+”在“var nytCount=+localStorage.getItem('nyt')| | 0;”中是什么意思?@zhank将值“强制转换”为一个数字
localStorage.getItem('nyt')
可能返回null
+0将返回0。它还可能包含无效值,例如字符串。在这种情况下,一元+
运算符将返回NaN
。0
和NaN
都是错误的,因此当OR运算符开始时,将计算右侧表达式,即0。最后的结果是,有效字符串被转换为数字,无效数字被替换为零。再次感谢。我已经使用了您的代码,但调试器似乎仍然不想在“chrome.storage.local.get('nyt',function(items)”之后插入大括号。现在看起来是这样的:var nytCount=+localStorage.getItem('nyt')| 0;console.log(nytCount);chrome.tabs.onUpdated.addListener(function(tabId,changeInfo,tab){var url=changeInfo.url;//字符串或未定义if(changeInfo.status='complete'&&url&&url&&url.indexOf(“NYTimes.com”)!=-1{localStorage.setItem('nyt',++nytCount);}
(很抱歉,太乱了。)@Zhankfor我在你的代码中没有看到任何调试器;
语句。如果你设置了断点,请确保它设置在包含var url=changeInfo.url;
的行上。如果你在它前面设置了断点,回调将丢失(以帮助你理解原因)。另外,请使用“nytimes.com”而不是“nytimes.com”。
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
});
});