我对web和chrome扩展开发很陌生,并试图使用localForage API为我的铬扩展存储数据--目前,每当我切换到一个新选项卡时,我都会丢失所有的数据,但我希望数据保留到用户显式清除所有内容(即使是多次会话)之前。
我决定让localForage api试一试(因为它应该像localStorage,但更简单),并且觉得我遗漏了一些重要的东西--我可以在没有问题的情况下设置it/getItems,但它实际上并没有保存任何数据。
如何确保在切换选项卡(以及多个浏览会话)时保留数据?
使用localForage.getItem/setItem- -这在使用数据方面似乎是有效的,但在切换选项卡时却没有做任何保存工作
citeUrl(values, function(citation)
{
count++;
var string = citation[0];
localforage.setItem(string, [citation[0],citation[1]], function(err, value)
{// Do other things once the value has been saved.
console.log(value[0] + " " + value[1]);
});
/*var result = document.getElementById('cite[]');
result.style.visibility = 'visible';
result.style.display = 'block';
result.innerHTML = citation[0];
renderStatus(citation[1]);*/
for(i = 0; i < count ; i++)
{
var newCitation = document.createElement('div');
localforage.getItem(citation[i], function(err, value)
{
newCitation.innerHTML = value[1] + "<br>" + value[0];
}
);
newCitation.style.backgroundColor = "white";
newCitation.style.marginBottom = "7px";
newCitation.style.padding = "6px";
newCitation.style.boxShadow= "0 2px 6px rgba(0,0,0,0.4)";
newCitation.style.borderRadius = "3px";
document.getElementById("answered[]").appendChild(newCitation);
}
}发布于 2016-01-26 10:40:10
localForage建立在localStorage和朋友的基础上。重要的是它与您访问它的来源绑定在一起。
从内容脚本中使用它将使用网站的起源,例如在http://example.com/test上使用扩展将数据绑定到http://example.com/源文件,而在http://example2.com/test上使用扩展将将数据绑定到附加到http://example2.com/的完全独立的存储。更重要的是,数据与页面本身的存储共享(并且可能会干扰)。
因此,使用localStorage (以及扩展而言,localForage)不允许在内容脚本中产生预期的结果(尽管如果您试图管理页面本身的存储,它可能仍然很有用)。
因此,有两种可能性可以正确地做到这一点:
localForage。在这种情况下,数据绑定到原点chrome-extension://yourextensionidhere。但是,这不能从内容脚本中访问--您需要使用消息传递传递数据,这是很麻烦的。chrome.storage。这将使人们可以轻松地在Chrome扩展和应用程序中使用它。显然,这就是已经尝试过的东西。这个问题可能是有用的。
https://stackoverflow.com/questions/35003546
复制相似问题