Chrome.storage.sync vs chrome.storage.local

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

+7
google-chrome google-chrome-extension
source share
2 answers

Oops!

The problem was that I was trying to sync data that was larger than the size. (4096 bytes per element)

I did not get chrome.runtime.lastError because I mistakenly put it in the get function area instead of the set function that generated the error. Therefore, I am posting this answer so that it can help others who share the same confusion.

You should check chrome.runtime.lastError inside every api call, for example:

 chrome.storage.local.set(objectToStore, function(data) { if(chrome.runtime.lastError) { /* error */ console.log(chrome.runtime.lastError.message); return; } //all good. do your thing.. } 

This works fine with chrome.storage.local , because according to the docs , you only have this limitation with sync .

printing chrome.runtime.lastError gave me: Object {message: "QUOTA_BYTES_PER_ITEM quota exceeded"}

+4
source share

The maximum size for local chrome storage is 5,242,880 bytes. To expand storage, you can add in manifest.json:

 "permissions": [ "unlimitedStorage" ] 

Maximum chrome sync storage size:

  • 102,400 bytes in total
  • 8 192 bytes per element
  • 512 items max
  • 1800 write operations per hour
  • 120 operations per minute

( source )

+9
source share

All Articles