我正在尝试保存用户在Chrome扩展程序弹出窗口中输入的一些信息。但是,当我尝试使用chrome.storage.sync.set
保存数据时,回调永远不会发生。我不知道为什么不能保存。
function saveNotebook () {
myNotebook.lastVisited = Date.now();
var jsonfile = {};
jsonfile[notebookURL] = myNotebook;
// jsonfile[notebookURL] = JSON.stringify(myNotebook);
chrome.storage.sync.set(jsonfile, function () {
if (chrome.extension.lastError) {
background.console.log('An error occurred: ' + chrome.extension.lastError.message);
}
background.console.log("Storing jsonfile: ", jsonfile);
background.console.log("Url for storage: ", notebookURL);
});
}
function getAllNotesForSite(notebookURL){
background.console.log("Retrieving storage for url: ",notebookURL);
chrome.storage.sync.get(notebookURL, function (noteBook){
background.console.log("Notebook Contents: ", noteBook);
if (Object.keys(noteBook).length === 0){
background.console.log("Notebook is empty");
myNotebook = {
lastVisited: "",
notes: {}
}
var date = new Date();
var noteId = "" + (date.getMonth()+1) +"/"+ (date.getDate());
createNewNote(noteId);
currentNoteId = noteId;
}else {
myNotebook = noteBook;
document.getElementById("last-visited").textValue = myNotebook.lastVisited;
for (var note in myNotebook.notes){
createNewNote(note.dateCreated);
}
}
});
}
background.console是我的背景页面(我认为这是合法的)。 这是我的manifest.json
{
"manifest_version": 2,
"name": "WebNotes",
"version": "1.0",
"description": "Annotate specific webpages or domains for later visits",
/*"icons": { "128": "icon_128.png" },*/
"background": {
"persistent": false,
"scripts": ["bg.js"]
},
"permissions": [
"activeTab",
"storage"
],
"browser_action": {
"default_title": "WebNotes",
/*"default_icon": "icon_19.png",*/
"default_popup": "popup.html"
}
}