Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading environment variables with Javascript

I need to read the system time from the environment variables on the client's OS. I searched on Stackoverflow and found that this isn't possible. However, those answers were from 2010 and I want to ask if anything has changed since.

Is it possible with some kind of framework? or Javascript is still sandboxed and cut off from OS?

like image 373
szpic Avatar asked Jan 23 '14 17:01

szpic


People also ask

Can Javascript read environment variables?

To retrieve environment variables in Node. JS you can use process. env. VARIABLE_NAME, but don't forget that assigning a property on process.

What is environment variables in JS?

Environment variables store variables, as the name implies. The values or things that get assigned to these variables could be API keys that you need to perform certain requests or operations. To create an environment variable, all you need to do is create a new file called .


3 Answers

szpic, if Javascript were not sandboxed from the OS, any half-witted criminal could get onto your computer and take control. Allowing environmental variables is way too low level to ever be allowed.

Take a look at all of your environmental variables and ask yourself if everyone on the planet should have access to all that data about your computer.


If you want to do this with Javascript, you'll have to use a special web browser, such as node-webkit. It isn't going to run on any normal web browser, though, so don't expect more than 0.0001% of the population to expect to run your page.


I don't know your intended use, but one trick, to throw in an environmental variable, is to make a shortcut to your web application, and include it in there as a URL parameter (at least on Winblows).

http://myapp.com?computername=%COMPUTERNAME%
like image 101
dthree Avatar answered Oct 14 '22 06:10

dthree


the javascript Date() Object provides the clients system time.

like image 7
kidconcept Avatar answered Oct 14 '22 05:10

kidconcept


FWIW.... Just playing around with an .HTA file (yes that uses the MSIE 'engine' so you can probably forget about other browsers) and the following JavaScript reads the %TEMP% path variable to set a filepath+name for safely writing a temporary file. This works OK for me :

var oTest = new ActiveXObject("wscript.shell");
pathTest = oTest.ExpandEnvironmentStrings("%TEMP%") + "\\test_it.txt";
oTest = null;
alert(pathTest);  // proves we can read %TEMP%

// and create the file, to complete the example
var oFSO = new ActiveXObject("Scripting.FileSystemObject");
var fil = oFSO.CreateTextFile(pathTest, true);
fil.WriteLine("Hello wonderful world!");
fil.Close();
like image 6
AjV Jsy Avatar answered Oct 14 '22 05:10

AjV Jsy