javascript 通过网页关闭计算机
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33628335/
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
Shutdown computer via web page
提问by Kevin
I have web based application which automatically loads on kiosk mode (full-screen) in chromium web browser on client's Computer wich is powered by Windows 7. I want to place a shutdown button on web page, so that user can directly shutdown the computer.
我有基于 Web 的应用程序,它在客户端计算机上的 Chrome 网络浏览器中自动加载信息亭模式(全屏),由 Windows 7 驱动。我想在网页上放置一个关闭按钮,以便用户可以直接关闭计算机。
- Is there any possibility to achieve this?
- What are the best solutions for both Windows & Linux?
- 有没有可能实现这一目标?
- Windows 和 Linux 的最佳解决方案是什么?
-P.S-
-PS-
I've full control over client's computer. (installing other software or browser extensions). it is a touch screen POS unit.
我可以完全控制客户的计算机。(安装其他软件或浏览器扩展)。它是一个触摸屏 POS 单元。
回答by Paul-Jan
As you have control over the target machine, this is completely feasible. One way to do it is by introducing a separate component to perform the shutdown.
由于您可以控制目标机器,因此这是完全可行的。一种方法是引入一个单独的组件来执行关闭。
Create a console application (using C#, for example) that listens to stdin, that shuts down the machine when it receives the right input. Machine shutdown from C# is not entirely trivial, but Stackoverflow knows how it is done.
Register this application with a proper manifest given the Google Chrome Native Messaging specification.
Call this extension from your webpage (on shutdownButton.click(), for instance) using Message Passing.
创建一个监听标准输入的控制台应用程序(例如使用 C#),当它接收到正确的输入时关闭机器。从 C# 关闭机器并不是一件小事,但Stackoverflow 知道它是如何完成的。
根据Google Chrome Native Messaging 规范,使用适当的清单注册此应用程序。
使用Message Passing从您的网页(例如在 shutdownButton.click() 上)调用此扩展程序。
The individual steps are a bit too broad to fully detail them in a single answer, but you can always open new questions on them if they give you any trouble.
各个步骤过于宽泛,无法在一个答案中完整详细说明,但如果它们给您带来任何麻烦,您可以随时提出新问题。
回答by Dirk-Jan
Because of security reasons this is simply not possible without running something outside of your browser.
由于安全原因,如果不在浏览器之外运行一些东西,这是不可能的。
If however the page is served from a server and you want to shutdown this server, keep reading:
但是,如果该页面是从服务器提供的,并且您想关闭该服务器,请继续阅读:
A solution which would be possible is to run a server in the background, for example node.js.
一种可能的解决方案是在后台运行服务器,例如 node.js。
On your node.js server you could listen for a specific url you are calling, on the trigger of this event you could send a shutdown command to your host. This however opens some security issues, you need to block this call to node from the outside (or not if you want others to be able to shut it down). And your node.js server has to be running with root rights which is also risky.
在您的 node.js 服务器上,您可以侦听您正在调用的特定 url,在此事件触发时,您可以向您的主机发送关闭命令。然而,这会带来一些安全问题,您需要阻止从外部对节点的调用(如果您希望其他人能够将其关闭,则不需要)。而且你的 node.js 服务器必须以 root 权限运行,这也有风险。
Take a look at https://github.com/typicode/stop-servermaybe you can find something in there that's usefull to you.
看看https://github.com/typicode/stop-server也许你可以在那里找到对你有用的东西。
回答by Elentriel
Your best bet as far as i can think would be to download a .bat that would shut down your computer and execute it, but aside from IE i'm not sure if it will work.
据我所知,您最好的选择是下载一个 .bat 文件,它会关闭您的计算机并执行它,但除了 IE 之外,我不确定它是否可以工作。
<script type="text/javascript" language="javascript">
function RunFile() {
WshShell = new ActiveXObject("WScript.Shell");
WshShell.Run("c:/windows/system32/notepad.exe", 1, false);
}
</script>
something along these lines, running the .bat file you have previously downloaded instead of notepad. Note, this will only work with IE.
沿着这些路线,运行您之前下载的 .bat 文件而不是记事本。请注意,这仅适用于 IE。
回答by Andrew Evt
You don't have directly access from WEB page to client's Computer, it's impossible. You only have access to the browser, but it is very limited.
你没有从WEB页面直接访问客户端的计算机,这是不可能的。您只能访问浏览器,但它非常有限。
You can only close window tab, using window.close()
. Read more about it here - https://developer.mozilla.org/en-US/docs/Web/API/Window/close
您只能关闭窗口选项卡,使用window.close()
. 在此处阅读更多相关信息 - https://developer.mozilla.org/en-US/docs/Web/API/Window/close