2

I'm currently trying to add some preferences to a Firefox add-on. To do so, I'm playing with the new "simple-prefs" module. (Simple-Prefs on the Mozilla Blog)

The documentation is not very detailed and I face some problems understanding how I can retrieve the value attached to an option and export it to a JS script present in my data folder.

Let's say that I have only one optional setting in my addon, a boolean one, then my packages.json will look like this:

{
  "name": "test",
  ...
  "preferences": [{
    "type": "bool",
    "name": "option1",
    "value": true,
    "title": "Desc Option 1" 
  }]
}

Here is my main.js file [UPDATED]:

var pageMod = require("page-mod");
const data = require("self").data;
const prefSet = require("simple-prefs");  //Simple-prefs module

var option1 = prefSet.prefs.option1;     //get the value for option1

function onPrefChange(prefName) {        //Listen for changes

   var prefName = prefSet.prefs[prefName];
                      }

prefSet.on("option1", onPrefChange);


exports.main = function() {
  pageMod.PageMod({ 
    include: ["https://mail.google.com/*","http://mail.google.com/*"],
    contentScriptWhen: 'ready',
    contentScriptFile: [data.url("jquery.js"),data.url("script.js")],
    onAttach: function(worker)
        {
      worker.postMessage( option1 );
        }
    });
}

How can I retrieve the value attached to "option1" and export it in order to call it in my "script.js" file?

1 Answers1

2

As usually, content scripts don't have access to the API - they can only receive messages from your extension's scripts. Here you would do:

pageMod.PageMod({ 
  include: ["https://mail.google.com/*","http://mail.google.com/*"],
  contentScriptWhen: 'ready',
  contentScriptFile: [data.url("jquery.js"),data.url("script.js")],
  onAttach: function(worker)
  {
    worker.postMessage(backtop);
  }
});

And in the content script you would have the following code:

self.on("message", function(data)
{
  alert("Received option value: " + data);
});

This message arrives asynchronously meaning that your content script won't know the option value initially - but that's how content scripts work.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • Last question: Any way to use postMessage all together with onPrefchange to be able to monitor changes in the settings and apply them to the script? See updated version of my main.js-- – user1229843 Feb 24 '12 at 18:40