views:

36

answers:

2

I am making an extension which should take a link as the user input only once. Then the entire extension keeps using that link on various functions in the JS file. When the user changes it, the value accessed by the js file also changes accordingly. I am using the following but it does not work for me

var pref_manager = Components.classes["@mozilla.org/preferencesservice;1"].getService(Components.interfaces.nsIPrefService)

function setInstance(){

   if (pref_manager.prefHasUserValue("myvar"))
{
   instance = pref_manager.getString("myvar");

    alert(instance);
} 
    if(instance == null){

instance = prompt("Please enter webcenter host and port");

    // Setting the value
pref_manager.setString("myvar", instance);

    }
}

instance is the global variable in which i take the user input. The alert (instance) does not show up, which means there is some problem by the way i am saving the pref or extracting it. Can someone please help me with this. I have never worked with preferences before. so even if there are minor problems i might not be able to figure out.

A: 

I don't have my own code in front of me to compare right now, but all of the above looks ok to me. My guess is that your problem is that you didn't set a default value for your preference, because I think Firefox will ignore preferences without a default value specified.

See:

https://developer.mozilla.org/en/Adding_preferences_to_an_extension

machineghost
A: 

use "pref_manager.getCharPref("prefname")" if the preference is a string. You could also have:

pref_manager.getBoolPref("prefname"); // boolean (true/false) preferences

and

pref_manager.getIntPref("prefname"); // numbers

Alex