Firefox SDK simple-storage and Firefox Sync

这一生的挚爱 提交于 2019-12-18 12:01:20

问题


I'm currently converting a Chrome extension into a Firefox add-on and would appreciate to replicate the chrome.storage.sync feature.

However, I cannot manage to find whether the data stored by a Firefox add-on using simple-storage will be automatically synced whenever a user of the add-on is signed into Firefox Sync.

Given that all the pieces of data stored via the latter method can be found in the user profile, I presume it will... as long as the add-on is available at https://addons.mozilla.org ?

Any information on the topic would be greatly appreciated.


回答1:


simple-storage is not synced. But you can sync it with little effort.

The trick it to store the storage object, it is serializable by definition, as a string preference and tell to the sync service to synchronize it.

Lets name that preference syncstorage and mark it as synchronizable.

var self = require("sdk/self");
var prefs = require("sdk/preferences/service");
prefs.set("services.sync.prefs.sync.extensions." + self.id + ".syncstorage", true);

When storing something to simple-storage reflect the change to syncstorage.

var sp = require("sdk/simple-prefs");
var ss = require("sdk/simple-storage");
sp.prefs["syncstorage"] = JSON.stringify(ss.storage);

For the opposite effect watch syncstorage for changes

sp.on("syncstorage", function(prefname){
  ss.storage = JSON.parse(sp.prefs["syncstorage"]);
})

Last but not least, It would nice and perhaps mandatory to sync only with the explicit consent of the user.



来源:https://stackoverflow.com/questions/23318396/firefox-sdk-simple-storage-and-firefox-sync

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!