I am working on an add-on as a project for Firefox and cannot get the hang of the onChange aspect of the 'simple-prefs' mod.
At the moment my add-on has to be disabled and then enabled again in order to activate any changes.
My code:
var pref = require("sdk/simple-prefs").prefs;
if (typeof onPrefChange == 'function') {
onPrefChange();
} else {
console.log(' Error Initiating')
}
const simplePrefs = require("sdk/simple-prefs");
simplePrefs.on("", onPrefChange);
function onPrefChange(prefName) {
tabs.on("ready", function(tab) {
var host = tabs.activeTab.url;
var worker = tabs.activeTab.attach({
contentScriptFile: self.data.url("docs.js")
});
worker.port.on("data", function(response) {
var mixedcont = response;
console.log(simplePrefs.prefs.dismixed + ' onchange in func')
if (mixedcont === "true" && host.indexOf("https:") == 0 &&
simplePrefs.prefs.dismixed === false &&
simplePrefs.prefs.disapp === false &&
pref.dismixed === false)
{
notificationmixed(host);
console.log(mixedcont + ' mixed content is found show notify ')
console.log(pref.dismixed + ' dismixed value from pref ')
console.log(simplePrefs.prefs.dismixed + ' dismixed value from SimplePrefs ')
console.log(host.indexOf("https:") + ' dismixed value from host ')
} else if (host.indexOf("https:") == 0 &&
mixedcont === "false" &&
simplePrefs.prefs.dissecure === false &&
simplePrefs.prefs.disapp === false &&
pref.dissecure === false)
{
notification(host);
} else {
console.log(host + ' could not determine ')
}
});
});
}
The conditional statements in my code call different functions depending on the simple-pref settings.
How can my addon activate these changes without having to be disabled and then enabled?
onPrefChange), so I'm not sure why would you expect it to execute on a pref change. Or is the problem that you don't get the message fromconsole.login onPrefChange? - Nickolayconsole.log? You need to apply the changes to your code in response to a change notification, not just on startup (this is when the top-level code with your conditionals executes). - NickolayonPrefChangelistener at all. Just move thetabs.onto the top level - it will read the newest prefs each time a tab is loaded. Right now you register a new duplicatetabs.onhandler each time a pref is changed, which might be the cause of your problems. - Nickolay