views:

181

answers:

1

Can we use a Global variable that persists over multiple instances of the browser (FF)? I am building a ff extension which has to take the host& port name from the user once and then execute the menu options accordingly using that very host and port. This Host and port must remain same untill the user resets it (for which an option will be given) On declaring the variable as global in the JS file, it would become null everytime the browser is restarted. Can anyone help me out with how and where to save this variable to get the desired functionality. Heres the code to set the preferences. but doesnt work for me function setInstance(){

if (pref_manager.prefHasUserValue("myvar"))

{ getString = pref_manager.getString("myvar");

instance = getString;

} if(instance == null){

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

// Setting the values

pref_manager.setString("myvar", instance);

pref_manager.setIntPref("myintvar", 1);

}

}

This function is called as soon as the extension menu option is opened. instance is a global variable which i need to be inputed by the user only once till reset

+1  A: 

You can store it in Firefox's preferences (so it gets stored in about:config and will be available every time Firefox is loaded).

var pref_manager = 
    Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

// Setting the values
pref_manager.setString("myvar", "mystring");
pref_manager.setIntPref("myintvar", 1);

// Getting the values
var getString = ""; // Default
if (pref_manager.prefHasUserValue("myvar"))
{
    getString = pref_manager.getString("myvar");
}
var getInt = 0; // Default
if (pref_manager.prefHasUserValue("myintvar"))
{
    getInt = pref_manager.getIntPref("myintvar");
}

You can find more information on the Mozilla Developer Center page for Preferences, and Adding Preferences To An Extension Page.

Rich Adams
Thanks Rich. but where should i put this code? i put it in the popup to my extension where it checks for the global variable if its null, it prompts for the user to input. it stays same over that instance of the browser. but when i restart it, again the same promp comes upfunction setInstance(){ if (pref_manager.prefHasUserValue("myvar")){ getString = pref_manager.getString("myvar"); instance = getString;} if(instance == null){instance = prompt("Please enter webcenter host and port");pref_manager.setString("myvar", instance);pref_manager.setIntPref("myintvar", 1); }
encryptor
i have put the code above in the question
encryptor
It's just used in your JavaScript file in /chrome/content/myext.js, etc. Wherever you need to use it. So maybe in your startup() function if you have one? Don't forget you need to get the preferences first too '(var pref_manager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);)'
Rich Adams
ya i am taking the preferences that way. then when the extension option is clicked, the popup for userinput is shown. I need to save this value. totally clueless on whats to be done! do you see an error in the above code?
encryptor