我需要否定/切换 chrome.storage.local 中变量的 bool 值,但它抛出错误:
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');
});
})
关于javascript - 如何否定 chrome.storage.local 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45859517/