Firefox 'simple-prefs' onchange issue

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:

function onPrefChange(prefName) {
  console.log("The preference " + 
              prefName + 
              " value has changed!");
}
require("sdk/simple-prefs").on("dismixed", onPrefChange);
require("sdk/simple-prefs").on("dissecure", onPrefChange);
require("sdk/simple-prefs").on("disapp", onPrefChange);

// listens to all changes
require("sdk/simple-prefs").on("", onPrefChange);

// Check prefs
var preferences = require("sdk/simple-prefs").prefs;

// check prefs

if (preferences.dismixed == true && preferences.disapp !== true)
  {
     dismixedtrue();

  } else if (preferences.dissecure == true && preferences.disapp !== true) {
        dissecure();

        } else if (preferences.disapp == true) {
        disapp();

    } else { 
    disnormal();
    }

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?

You should try moving the code that checks the preferences (the if statements in this code snippet) into the onPrefChange function.

These three lines are redundant, as after all, you add a listener for all preferences changes:

require("sdk/simple-prefs").on("dismixed", onPrefChange);
require("sdk/simple-prefs").on("dissecure", onPrefChange);
require("sdk/simple-prefs").on("disapp", onPrefChange);

You shouldn’t call require this much. Just call it once, and keep a reference to the module that you may use later:

const simplePrefs = require("sdk/simple-prefs");
simplePrefs.on("", onPrefChange);

And as @aaronraimist said, move your checks to onPrefChange

Thanks for your help guys.

I have changed the code to:

function onPrefChange(prefName) {
    const simplePrefs = require("sdk/simple-prefs");
       simplePrefs.on("", onPrefChange);
   
 if (simplePrefs.dismixed == true && simplePrefs.disapp !== true)
  {
     dismixedtrue();
  
  } else if (simplePrefs.dissecure == true && simplePrefs.disapp !== true) {
        dissecure();
        
        
        } else if (simplePrefs.disapp == true) {
        disapp();
        

    } else { 
    disnormal();
   
    }
  console.log("The preference " + 
              prefName + 
              " value has changed!");
}

Now I can disable part of the app without having to disable and then enabling (which is what the sime-pref is for) but have to disable and then enable the add-on to enable part of the app if that makes sense? Am I missing something in the code maybe to check for a false value in the conditions?

Also the value simplePrefs is coming back as undefined? Sorry guys still learning

Still it needs a couple of changes.
These two lines:

const simplePrefs = require("sdk/simple-prefs");
simplePrefs.on("", onPrefChange);

Have to be moved outside of onPrefChange.

Also, inside onPrefChange, you have to use simplePrefs.prefs to access a preference (for example simplePrefs.prefs.dismixed)

simplePrefs holds a reference to the module. Inside the module, there’re a couple of members that you may read about here:

And yes, every time a user changes one of your addon preferences, your onPrefChange function will be called and you may make your addon react to the preferences modification.

My GPL’d toolbar’s index.js uses simple-prefs for a chunk, and can be found at http://npgpltoolbar.sourceforge.net/ Take a peek and see if it helps.

Thanks again guys for your help. Still having the same issue as my last post though with this new code:

const simplePrefs = require("sdk/simple-prefs");
simplePrefs.on("", onPrefChange);     
     

onPrefChange();     
     
function onPrefChange(prefName) {
    
     //console.log("The preference " + prefName + " value has changed!");
     
    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 + ' mixed content value for onchange in func')
    
     if (mixedcont == "true" && host.indexOf("https:") == 0 && simplePrefs.prefs.dismixed !== true && simplePrefs.prefs.disapp !== true)
  {
    notificationmixed(host);
    console.log(mixedcont + '  found show notify ')
    
  } else if (host.indexOf("https:") == 0 && mixedcont !== "true" && simplePrefs.prefs.dissecure !== true && simplePrefs.prefs.disapp !== true) {

        notification(host);
        console.log(host + ' green light "')

    } else { 
    console.log(host + ' could not determine ')
    }
                              });
    }); 
}

It must be something simple that I am doing wrong here?

It will disable part of the add-on without having to disable and then enabling the add-on, however will not enable part of the add-on without having to disable and then re-enabling the add-on?