views:

143

answers:

2

Hi,

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 extension update?

Thanks

+2  A: 

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.

Arne Roomann-Kurrik
In fact, `localStorage` is preserved even if the user uninstalls the extension, then installs it again. However, in Chrome 4 clearing browser data/cache will wipe it out.
Max Shawabkeh
+1  A: 

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.

kodra