store preferences in google chrome extentions
Using firefox, I can store extension preferences using Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefService);
What interface can I use to store preferences in Chrome? And do they get overwritten on an exte开发者_C百科nsion update?
Thanks
You can use the localStorage API. See http://code.google.com/chrome/extensions/options.html for an example of building an options page with this, but most simply you can do:
localStorage['foo'] = 'bar'; // Store data
var foo = localStorage['foo']; // Get data
This data won't be wiped out on extension update, either.
The new HTML5 feature to persist data locally (localStorage) should help you here.
In addition to coockies, now there are session and local storage objects, as well as, a simple version of relational database (all storing data on your local harddisk).
One hint when storing/retrieving objects to localStorage ... it is a simple implementation of the Map interface, so the safest way to persist your object is by serializing them via JSON.stringify function and parse retrieved string with JSON.parse.
精彩评论