In xul, how to retrieve an environment variable?

2019-07-17 07:19发布

问题:

I have an environment called $REP, how can I access the value of this variable using Xulrunner in a Linux environment?

--udpate

attempting with nslEnvironment:

var env = Components.classes["@mozilla.org/process/environment;1"].getService(Components.interfaces.nsIEnvironment);
dump("bash=" + env.exists("BASH") + '\n');
dump("bash=" + env.exists("$BASH") + '\n');

the output was:

bash=false
bash=false

as you imagine, it should output "/bin/bash" as it does in the terminal. I also tried using get to see if it was just the exists method wrong, but it returned empty.

What can be wrong here?

回答1:

Use nsIEnvironment to read (and set) environment variables.



回答2:

Have a look at Setting an environment variable in javascript (which you happened to edit, randomly!).

I very much doubt you'll be able to read these variables from a browser environment though.

edit:

var oShell = WScript.CreateObject("WScript.Shell");
var oSysEnv = oShell.Environment("SYSTEM");
WScript.Echo (oSysEnv("PATH"));

perhaps?