Firefox SDK simple-storage and Firefox Sync

后端 未结 1 546
情书的邮戳
情书的邮戳 2020-12-29 17:52

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 fin

1条回答
  •  囚心锁ツ
    2020-12-29 18:29

    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.

    0 讨论(0)
提交回复
热议问题