【发布时间】:2016-01-25 22:21:34
【问题描述】:
我是 web 和 chrome 扩展开发的新手,我正在尝试使用 localForage API 来存储我的 chrome 扩展的数据 - 目前我每次切换到新标签时都会丢失所有内容,但我希望数据一直保留到用户明确清除所有内容(因此即使在多个会话等)
我决定试一试 localForage api(因为它应该类似于 localStorage,但更简单),感觉我遗漏了一些重要的东西——我可以毫无问题地 setItems/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);
}
}
【问题讨论】:
标签: javascript google-chrome google-chrome-extension local-storage localforage