javascript - Chrome 扩展存储删除不起作用
问题描述
我正在构建 chrome 扩展程序,但 chrome.storage.sync.remove 有问题
假设这是我的 chrome.storage 的内容:
这包含我要删除的项目 (removedItems[])
这是我的代码:
chrome.storage.sync.get(null, function(data) {
coasterList = data;
console.log('FFFFFFFF :',coasterList.data);
chrome.storage.sync.remove(removedItems[0].CoasterName, function(data) {
chrome.storage.sync.get(null, function(data) {
var coasterListFINAL = data;
console.log('FINAL LIST :',coasterListFINAL.data);
//console.log(removedItems[0].CoasterName);
});
});
});
当我这样做时什么都没有发生:
chrome.storage.sync.remove(removedItems[0].CoasterName, function(data) {...}
我究竟做错了什么 ?(我没有错误,但我要删除的密钥仍然在这里)
解决方案
您的代码:
chrome.storage.sync.set({'CoasterList':coasterListClean}, function() {
console.log("SAVED");
});
/*
if you try to retrieve the newly set storage var "CoasterList" right after setting it this way
you will probably get the old value 'cause you are reading something that is not changed yet
ALL CHROME.STORAGE APIS ARE ASYNCHRONOUS!!!
*/
chrome.storage.sync.get(null, function(data) {
console.log(data.coasterList);
});
----------------------------------
/*
if you want to retrieve the new value of CoasterList you have
to get it inside the storage.sync.set callback function.
if you are under MV3 rules you can also use the "promise returned" syntax
THIS SHOUL WORK
*/
chrome.storage.sync.set({'CoasterList':coasterListClean}, function() {
console.log("SAVED");
chrome.storage.sync.get(null, function(data) {
console.log(data.coasterList);
});
});
推荐阅读
- python - 从PDF中提取特定单词周围的文本
- angular - 将 Angular 4+ 集成到 HTML/现有网络应用程序
- c - 如何通过添加指针来修复我的 C 代码?
- harmon.ie - 最近更新后,Outlook 中的 Harmon.Ie 侧边栏为空白
- node.js - Lodash - 深度类型验证
- php - 需要使 WordPress 表单提交触发在其他站点上注册新用户的过程
- python - 如何在 Linux 中从 C 语言执行 Python 程序
- perl - 循环文件时检查 EOF
- regex - 从文件名中提取域
- python - 如果没有 setup.py 文件,下载后如何安装 GitHub 包