开发者

store preferences in google chrome extentions

开发者 https://www.devze.com 2022-12-21 19:56 出处:网络
Using firefox, I can store extension preferences using Components.classes[\"@mozilla.org/preferences-service;1\"]

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号