I was trying to figure out how to use chrome.storage.api. I have included the following in manifest.json :
"permissions": [ "activeTab","storage" ],
Than I opened a new tab with devtools and switched <page context> to one of my chrome extensions. What I typed:
chrome.storage.sync.set({"foo":"bar"},function(){ console.log("saved ok"); } );
and received:
undefined saved ok
How did I try to get this stored value:
chrome.storage.sync.get("foo",function(data){ console.log(data); } );
but it bothered me:
undefined Object {}
Than I did the same, but instead of sync used local , and it worked as expected:
chrome.storage.local.set({"foo":"bar"},function(){ console.log("saved ok"); } );
.. and search:
chrome.storage.local.get("foo",function(data){ console.log(data); } );
Which got me: Object {foo: "bar"} as it should be.
Is it that I am not registered in my chrome account? But in this case, is not chrome.storage.sync designed to back up to local data storage?
EDIT
It's strange when I type this directly on the console it seems to work, but this code does not run from the background.js code inside the click listener:
var dataCache = {}; function addStarredPost(post) { var id = getPostId(post); var timeStamp = new Date().getTime(); var user = getUserName(); dataCache[id] = {"id":id,"post":post,"time":timeStamp,"user":user}; chrome.storage.sync.set(dataCache,function(){ console.log("Starred!");}); }
After that, chrome.storage.sync.get(null,function(data){ console.log(data); }); returns an empty object, as if the data had not been saved.: / This code seems to work perfectly with chrome.storage.local .
chrome.runtime.lastErros returns undefined