我需要否定/切换 chrome.storage.local 中变量的布尔值,但它抛出一个错误:
var toggle = chrome.storage.local.get('toggle');
chrome.storage.local.set({ "toggle": !toggle }, function() {
console.log('Settings saved');
});
我尝试先获取变量,但它显示了一个错误:表单 get(string) 的调用与定义 get 不匹配(可选字符串或数组或对象键,函数回调)。任何帮助表示赞赏!
由于读取是异步的,我们需要使用回调(因此出现错误消息):
chrome.storage.local.get("toggle", function(data) { // async callback
// check for errors via runtime.lastError
// update settings
chrome.storage.local.set({"toggle": !data.toggle}, function() { // invert and set
// check for errors via runtime.lastError
// Notify that we saved.
console.log('Settings saved');
});
})
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句