我正在开发非常简单的扩展。我的 popup.html 有一些单选按钮,它们代表用户的一组预定义选项。问题是我的内容脚本的逻辑取决于这些选项。
所以,我的问题是: 让我们假设我将一个脚本附加到“popup.html”(符合 Chrome CSP),该脚本会监听我的单选按钮选择中的任何更改,并且一旦发生类似 .localstorage.setItem(key, value) 之类的操作?我的内容脚本能够在适当的时候从本地存储获取这个值吗?
最佳答案
当然,你可以这样做。这里有两个例子:
chrome.storage.local.set({'value': theValue}, function() {
// Notify that we saved.
message('Settings saved');
});
chrome.storage.local.get('value', function(obj) {
//Notify that we get the value.
message('Value is ' + obj.value);
});
或者您可以使用同步存储:
chrome.storage.sync.set({'value': theValue}, function() {
// Notify that we saved.
message('Settings saved');
});
chrome.storage.sync.get('value', function(obj) {
//Notify that we get the value.
message('Value is ' + obj.value);
});
这里是the documentation .
chrome.storage
和 window.localStorage
之间的区别在于,您可以在每个最新的浏览器上使用 window.localStorage
。也在网站上。存储的对象将一直存储到用户关闭浏览器为止。
您正在构建一个 Chrome 扩展程序,因此我建议您使用为此构建的 API。
关于javascript - 我可以将 popup.html 中的输入数据保存在 localStorage 中吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39804373/