如果javascript尚未运行,请运行javascript [英] Getting javascript to run a process if it's not already running

查看:114
本文介绍了如果javascript尚未运行,请运行javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果已经没有运行,我想让Javascript运行说cmd.exe。

I want to have Javascript run say cmd.exe if there is already not one running.

我希望有一种方法让javascript看看正在运行的进程然后如果名称在列表中不运行。但如果它没有运行过程。

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不是脚本操作系统级过程控制的最佳途径。如果javascript能够直接访问您的操作系统,那么浏览互联网将是一个极大的安全风险。

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.

Internet Explorer确实有一个脚本机制来自javascript的Windows,但您必须调整安全设置才能允许这种情况发生。其他浏览器甚至不提供这种可能性。

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中执行notepad.exe:

This code will execute notepad.exe in Internet Explorer, after choosing to "Allow blocked content" from the security warning:

var shell = new ActiveXObject('WScript.Shell');
shell .Run("notepad.exe");

docs: http://msdn.microsoft.com/en-us/library/aew9yb99%28v=vs.85%29.aspx

因此,我们可以使用此方法列出活动流程,以便在适当时启动一个:

So, we can use this method to both list active processes and to 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');

请记住,这是一个概念的证明 - 在实践中我不会建议你永远,永远这样做。它是特定于浏览器的,危险的,可利用的,并且通常是不好的做法。 Web语言适用于Web应用程序,尽管Microsoft可能想要告诉您,但javascript并不是一种操作系统脚本语言。 :)

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. :)

这篇关于如果javascript尚未运行,请运行javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆