I'm developing a new version of my add-on (targeting Gmail) using the SDK and would need to store a few boolean preferences for my users. To do so, I would like to play with the new "simple-prefs" API. Based on the Mozilla documentation and this Stackoverflow question, I got something but I face the following issues:
1) I cannot get the change of my preferences to be properly passed to my external script in data. I need to disable and re-enable the add-on to get the changes applied.
2) How to ensure that the preferences are directly applied to the tab in which Gmail is opened.
Here is my package.json:
{
"name": "Demo",
"license": "MPL 1.1/GPL 2.0/LGPL 2.1",
"author": "me",
...
"preferences": [{
"type": "bool",
"name": "option1",
"value": true,
"title": "Description Option1"
},
{
"type": "bool",
"name": "option2",
"value": true,
"title": "Description Option2"
}]
}
My main.js:
const pageMod = require("page-mod");
const data = require("self").data;
const prefSet = require("simple-prefs");
// Get the values for options
var option1 = prefSet.prefs.option1;
var option2 = prefSet.prefs.option2;
// Listen for changes
function onPrefChange(prefName) { prefSet.prefs[prefName]; }
prefSet.on("option1", onPrefChange);
prefSet.on("option2", 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);
worker.postMessage(option2);
}
});
}
My script.js (in data):
(function(){
// Option 1
self.on("message", function(option1) {
if( option1 == true ) {
// Do this
}});
// Option 2
self.on("message", function(option2) {
if( option2 == true ) {
// Do this
}});
})();
There is certainly a neater way to use the postMessage protocol in my main.js file and to call it in my script.js file... Feel free to suggest your ideas and suggestions!