1
votes

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?

1
Your code that actually reads the preferences is not in any kind of callback (like 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 from console.log in onPrefChange? - Nickolay
The problem I am having is that if a user ticks a box within "options" (Firefox add-on) in order to disable a certain notification, in order for that setting to take effect the user must first disable my add-on and then re-enable. - DaE
OK, so do you see the message you log with console.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). - Nickolay
Please indent your code properly, otherwise it's hard to read. This new version looks fine, except you don't need the onPrefChange listener at all. Just move the tabs.on to the top level - it will read the newest prefs each time a tab is loaded. Right now you register a new duplicate tabs.on handler each time a pref is changed, which might be the cause of your problems. - Nickolay
BINGO! Works perfect thank you so much - DaE

1 Answers

1
votes

To sum up the solution we came to in the comments:

1) You don't have to use require("sdk/simple-prefs").on() if your preferences do not affect state of the add-on (visibility of an UI element, styling etc.) and only affect the way your code reacts to events.

In this case you can just retrieve the current value from the preferences via require("sdk/simple-prefs").prefs.* each time you process an event.

2) If you read a value from preferences and store it somewhere (in a variable in your code, by modifying the DOM or a widget state), you have to update it properly from an on() handler.

In order to do that, you must ensure you know all the places that depend on this preference and update each one either directly from the on() handler (this could work for a global variable, for example) or indirectly (e.g. by passing the appropriate message to the content script).

You shouldn't cache the preference value in a local variable of a long-lived function (such as tabs.on("ready", function() { var myCachedPref = ...; });) as you won't be able to update it from an outside handler. You can work around that by putting a separate pref change handler inside the long-lived function:

    tabs.on("ready", function(tab) {
      var cachedPref = simplePrefs.prefs.myPref;
      simplePrefs.on("", function(prefName) {
        //update cachedPref as needed
      });