如果某个进程尚未运行,则让 javascript 运行它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6833419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting javascript to run a process if it's not already running
提问by Dennis Hayden
I want to have Javascript run say cmd.exe if there is already not one running.
如果已经没有运行,我想让 Javascript 运行说 cmd.exe。
I was hoping there is a way to have javascript look at running processes and then if the name is in the list dont run. but if it's not run the process.
我希望有一种方法可以让 javascript 查看正在运行的进程,然后如果名称在列表中,则不要运行。但如果它不运行该过程。
回答by Chris Baker
Javascript is not the best route for scripting OS-level process control. If javascript were able to have such direct access to your operating system, it would be an extreme security risk to ever browse the internet.
Javascript 不是编写操作系统级过程控制脚本的最佳途径。如果 javascript 能够直接访问您的操作系统,那么浏览互联网将是一个极大的安全风险。
Internet Explorer does have a mechanism to script Windows from javascript, but you would have to adjust your security settings to allow this to occur. Other browsers do not even offer the possibility.
Internet Explorer 确实具有从 javascript 编写 Windows 脚本的机制,但您必须调整安全设置以允许这种情况发生。其他浏览器甚至不提供这种可能性。
This code will execute notepad.exe in Internet Explorer, after choosing to "Allow blocked content" from the security warning:
在从安全警告中选择“允许被阻止的内容”后,此代码将在 Internet Explorer 中执行 notepad.exe:
var shell = new ActiveXObject('WScript.Shell');
shell .Run("notepad.exe");
docs: http://msdn.microsoft.com/en-us/library/aew9yb99%28v=vs.85%29.aspx
文档:http: //msdn.microsoft.com/en-us/library/aew9yb99%28v=vs.85%29.aspx
So, we can use this method to both list active processes andto start one if it is appropriate:
因此,我们可以使用此方法列出活动进程并在适当时启动一个:
function startUniqueProcess(process_name, process_path) {
// create a shell object and exec handle
var shell = new ActiveXObject('WScript.Shell');
var handle = shell.Exec("tasklist.exe");
// loop through the output of tasklist.exe
while (!handle.StdOut.AtEndOfStream) {
// grab a line of text
var p = handle.StdOut.ReadLine();
// split on space
p = p.split(' ');
// check for split lines longer than 2
if (p.length < 2)
continue;
// match process_name to this item
if (p[0] == process_name) {
// clean up and return false, process already running
shell = null;
handle = null;
return false;
} // end :: if matching process name
} // end :: while
// clean up
handle = null;
// process not found, start it
return shell.Exec(process_path);
}
// example use
var result = startUniqueProcess('notepad.exe', 'notepad.exe');
if (result === false)
alert('did not start, already open');
else
alert('should be open');
Keep in mind, this is proof of a concept - in practice I would not suggest you ever, ever, ever do this. It is browser-specific, dangerous, exploitable, and generally bad practice. Web languages are for web applications, javascript is not intended to be a OS scripting language despite what Microsoft might want to tell you. :)
请记住,这是一个概念的证明 - 在实践中,我不会建议您永远,永远,永远不要这样做。它是特定于浏览器的、危险的、可利用的,并且通常是不好的做法。Web 语言用于 Web 应用程序,尽管 Microsoft 可能想告诉您,javascript 并不打算成为操作系统脚本语言。:)