seb68
seb68

Reputation: 269

Firefox add-on sdk : how to use simple-storage API in a content script?

I am making a Firefox add on using toolbar button by Erik Vold and I would like to know how to have access simple-storage API in a content script. I know that you cannot access the API directly.

var data = require('self').data;
var tabs = require('tabs');
var pageMod = require('page-mod');
var ss = require('sdk/simple-storage');

    var tbb = require('toolbarbutton').ToolbarButton({
      id: 'button',
      label: 'us-button',
      image: data.url('img/on.png'),
      onCommand: function () {

        tabs.activeTab.attach ({

            contentScriptFile: [
                data.url('jquery/jquery.min.js'),
                data.url('jquery/jquery-ui.js'),
                data.url('recuperation.js'),
                data.url('dialog.js')
          ],
          contentScriptOptions: {
          imgfr: data.url('img/fr.png'),
          imgen: data.url('img/en.png'),
          imglogo: data.url('img/logo.png')
        }
       });
      }
    });

My question is : how to have access to simple-storage API in my content script dialog.js

Upvotes: 3

Views: 1443

Answers (1)

erikvold
erikvold

Reputation: 16528

tabs.activeTab.attach (...) returns a worker with which you can use to sent messages between the content script and the addon. With this you can have the content script send messages to the addon about what data to store in simple-storage.

So the addon side would look like:

let worker = tabs.activeTab.attach(...);
worker.port.on('simple-storage', function(newData) {
  ss.storage = newData;
});

and the content script side would look like:

self.port.emit('simple-storage', newData)

when you have newData to save.

Upvotes: 1

Related Questions