我正在尝试使用chrome.storage.sync
这是我的代码:
chrome.commands.onCommand.addListener(function(command) {
if (command == "Save And Sleep") {
let time_ = new Date().toISOString();
chrome.windows.getAll({
populate: true
}, function(windows) {
//console.log(windows);
windows.forEach(function(window) {
let data_ = JSON.stringify(window);
let key_ = "MUA-Save&Sleep " + time_ + " " + window.id;
var obj = {};
obj[key_] = data_;
console.log(data_);
chrome.storage.sync.set(obj, function() {
console.log(data_ + "saved");
});
});
});
} else if (command == "Restore And Breath") {
//let json_=JSON.parse(JSON.stringify(window));
chrome.storage.sync.get(null, function(items) {
var allKeys = Object.keys(items);
//console.log(allKeys);
//console.log(allKeys[1]);
allKeys.forEach(function(key) {
if (key.startsWith("MUA-Save&Sleep") == true) {
chrome.storage.sync.get([key], function(result) {
console.log('Value currently is --- ' + result);
//console.log(key);
});
}
});
});
}
});
在chrome.storage.sync.set
内部,它说它已经保存了值,但是当我尝试检索这些值时,它什么也没返回。
这很简单:
这是怎么了?