如果进程尚未运行,则让 javascript 运行该进程

发布于 2024-11-26 10:53:47 字数 114 浏览 1 评论 0原文

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

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

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寂寞清仓 2024-12-03 10:53:47

JavaScript 并不是编写操作系统级过程控制脚本的最佳途径。如果 javascript 能够直接访问您的操作系统,那么浏览互联网将面临极大的安全风险。

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

从安全警告中选择“允许阻止的内容”后,此代码将在 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

因此,我们可以使用此方法来列出活动进程<并在适当的情况下启动一个:

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 并不是一种操作系统脚本语言。 :)

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

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');

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文