简体   繁体   English

使用Javascript读取环境变量

[英]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. 我在Stackoverflow上搜索,发现这是不可能的。 However, those answers were from 2010 and I want to ask if anything has changed since. 然而,这些答案来自2010年,我想问一下自那以后是否有任何改变。

Is it possible with some kind of framework? 是否可以使用某种框架? or Javascript is still sandboxed and cut off from OS? 或Javascript仍然是沙盒并从操作系统切断?

szpic , if Javascript were not sandboxed from the OS, any half-witted criminal could get onto your computer and take control. szpic ,如果Javascript没有来自操作系统的沙盒,那么任何半szpic犯罪分子都可以进入你的计算机并控制住。 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 . 如果您想使用Javascript执行此操作,则必须使用特殊的Web浏览器,例如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. 但是,它不会在任何普通的Web浏览器上运行,所以不要指望超过0.0001%的人口期望运行您的页面。


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). 我不知道你的预期用途,但抛出环境变量的一个技巧是创建一个Web应用程序的快捷方式,并将其作为URL参数包含在那里(至少在Winblows上)。

http://myapp.com?computername=%COMPUTERNAME%

javascript Date()对象提供客户端系统时间。

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. FWIW ....只是玩一个.HTA文件(是的,使用MSIE'引擎',所以你可能会忘记其他浏览器) ,以下JavaScript读取%TEMP%路径变量来安全地设置文件路径+名称写一个临时文件。 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();

There is only one way to do this - by using special web browser. 只有一种方法可以做到这一点 - 使用特殊的Web浏览器。 Something like node-webkit . node-webkit这样的东西。 It integrates webkit engine with node.js, so you can use it together with DOM. 它将webkit引擎与node.js集成在一起,因此您可以将它与DOM一起使用。

document.write(require('os').type());

But as everybody else said - there is no chance to do it with "normal" web browser. 但正如其他人所说 - 没有机会用“普通”的网络浏览器来做。

This was possible using VBScript but support is removed in IE11. 这可以使用VBScript,但在IE11中删除了支持。 If you are in an IE environment (such as an internal Intranet) then you can convert your VB Code (CreateObject("wscript.shell")) to use ActiveX instead. 如果您在IE环境(例如内部Intranet)中,则可以将VB代码(CreateObject(“wscript.shell”))转换为使用ActiveX。 This will get you around the issue that VB Script is depreciated in IE11 Edge mode and allow your page to access your environment variables and be used in Edge mode. 这将解决您在IE11边缘模式下弃用VB脚本的问题,并允许您的页面访问您的环境变量并在边缘模式下使用。 Obviously this is IE only but if you were using VBScript before it was also IE only. 显然这只是IE浏览器,但如果您使用的是VBScript,那么它也只是IE浏览器。

var ax = new ActiveXObject("wscript.shell");
uname = ax.ExpandEnvironmentStrings("%USERNAME%");
av = null;
alert(uname);

All of the answers are today (at the time of writing) long outdated. 今天(在撰写本文时)所有的答案都已过时了。 :) :)

Of course, JS is still sandboxed, but that does not prevent anyone from using something like environment variables in client software. 当然,JS仍然是沙箱,但这并不妨碍任何人在客户端软件中使用类似环境变量的东西。 What you need to know and use wisely is the transfer of data of any kind from the operating system into a front-end application. 您需要明智地了解和使用的是将任何类型的数据从操作系统传输到前端应用程序。

Several front-end frameworks may help here. 几个前端框架可能会有所帮助。 Eg an Angular application created with @angular/cli provides use cases for different environments out of the box. 例如,使用@ angular / cli创建的Angular应用程序为开箱即用的不同环境提供了用例。 In an application configuration file (eg angular.json) you can configure the options, variables, values, whatever you need that you transport into your front end application via eg webpack . 在应用程序配置文件(例如angular.json)中,您可以配置选项,变量,值,以及通过Webpack传输到前端应用程序所需的任何内容 They may only be read once and can not change at runtime. 它们只能被读取一次,并且不能在运行时更改。

Following library nwjs.io tries something new: it lets you call all Node.js modules directly from DOM. 以下库nwjs.io尝试新的东西:它允许您直接从DOM调用所有Node.js模块。

In docked/docker environments, there are other expectations for backend and frontend software, and these environments are configured differently and properly constrained from the OS's point of view. 在停靠/ docker环境中,对后端和前端软件还有其他期望,并且这些环境的配置不同,并且从操作系统的角度进行了适当的约束。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM