This site will have limited functionality while we undergo maintenance to improve your experience. If an article doesn't solve your issue and you want to ask a question, we have our support community waiting to help you at @FirefoxSupport on Twitter and/r/firefox on Reddit.

ابحث في الدعم

Avoid support scams. We will never ask you to call or text a phone number or share personal information. Please report suspicious activity using the “Report Abuse” option.

Learn More

Is it possible to user Windows environment variables in Firefox preferences?

  • 5 ردود
  • 2 have this problem
  • 2 views
  • آخر ردّ كتبه NukemBy

more options

Is it possible to use Windows environment variables (e.g. %ProgramFiles% or %APPDATA%) in Firefox preferences? If not, are there native equivalents?

Is it possible to use Windows environment variables (e.g. %ProgramFiles% or %APPDATA%) in Firefox preferences? If not, are there native equivalents?

All Replies (5)

more options

The prefs.js and user.js file can only contain pref settings and not additional JavaScript.

So you would need to do this via a mozilla.cfg file to specify the pref.

Place a file local-settings.js in the defaults\pref folder where you also find the file channel-prefs.js to specify using mozilla.cfg.

pref("general.config.filename", "mozilla.cfg");
pref("general.config.obscure_value", 0); // use this to disable the byte-shift

See:

You can use these functions in mozilla.cfg:

defaultPref();  // set new default value
pref();         // set pref, but allow changes in current session
lockPref();     // lock pref, disallow changes

//
const Cc=Components.classes,Ci=Components.interfaces;
envUN=Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment).get('USERNAME');

pref("environment.username", envUN);
more options

It works! Thanks. But the preference I want to set is located in a plain vanilla preferences file bundled with an add-on. Is there a way to set a variable in mozilla.cfg which could then be utilized in a plain vanilla preferences file?

more options

I think that you will have to create an extension if you want to do this with normal prefs and convert the value of the pref to resolve the environment variables to absolute values.

Can you give an example of what you try to do?

more options

I'm using an extension with a preference that allows the name and location of its data file to be specified, for example:

pref("extensions.name.dataDir", "C:\Stuff\Data");

I don't want to modify the author's preferences file, so I put the preference in a separate file in the same directory. Everything works fine when I specify an absolute path, but I'd like to do something like:

pref("extensions.name.dataDir", "%APPDATA%\Data");

It seems that it isn't possible to directly utilize Windows environment variables in Firefox preferences files. Pity. Your suggestion seems to work, though I have some concerns regarding the order in which the mozilla.cfg file and the prefs.js files are processed. And I would prefer to handle this in my custom prefs.js file rather than in mozilla.cfg, just so that all related preferences remain grouped together.

I was hoping that I could use your suggestion to set some variables that I could use elsewhere, similar to the way that preference "general.useragent.locale" sets the value of %LOCALE%. Or maybe there's a way to reference the value of one preference from another, for example: pref("pref1", "(value of pref2)"). Or maybe you already have a solution; this issue must have come up before.

Thanks for your help.

more options

I've tried solution from cor-el and it miss some details

  • correct location of mozilla.cfg is <Firefox install dir>\App\Firefox\mozilla.cfg
  • mozilla.cfg can concatenate string, this is not possible in prefs.js
  • value defined in mozilla.cfg overwrites value defined in prefs.js - as far as i understand this happens at startup of firefox

Here is the configuration to overwrite location of the cache directory for example

.\App\Firefox\mozilla.cfg:

//
const Cc=Components.classes,Ci=Components.interfaces;
USERNAME=Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment).get('USERNAME');
LOCALAPPDATA=Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment).get('LOCALAPPDATA');

pref("environment.username", USERNAME);
pref("environment.localappdata", LOCALAPPDATA);
pref("browser.cache.disk.parent_directory", LOCALAPPDATA + '\\MozillaFirefox');

Modified by cor-el