Javascript 控制台应用程序?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11001203/
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
Javascript Console Application?
提问by Navweb
Is it possible to make a console based application in JS?
This sort of all I can achieve right now.
I was hoping for a in-browser console based application rather than a popup prompt :P
是否可以在 JS 中制作基于控制台的应用程序?
这就是我现在所能达到的。我希望有一个基于浏览器控制台的应用程序,而不是弹出提示:P
var fname=prompt("Command:","")
if (fname=="do_stuff") {alert("Ok, i will do stuff...")}
else {alert("You did not tell me something i know!")};
In the Javascript Program I have composed above: there is first a prompt
box then there
is an if
statement to check if the user entered "do_stuff" if he does not..("else
") then it will display an alert box: You did not tell me something I know!
在我上面编写的 Javascript 程序中:首先有一个prompt
框,然后有一个if
语句来检查用户是否输入了“do_stuff”,如果他没有输入 ..(" else
") 然后它会显示一个警告框:你没有告诉我的东西我知道!
采纳答案by Brendan
Short answer: you can do just about anythingusing JavaScript. Here is an example console that takes a command followed by a space-delimited set of arguments, similar to many windows command line actions.
简短的回答:您可以使用 JavaScript做任何事情。这是一个示例控制台,它接受一个命令,后跟一组以空格分隔的参数,类似于许多 Windows 命令行操作。
It seems like you're very new to JavaScript, and you should learn to learn how it works with the browser at its core… but I don't know what browser you're going to be using, so my example uses the YUI framework so my code isn't cluttered with browser normalization.
看起来你对 JavaScript 很陌生,你应该学习学习它是如何与浏览器一起工作的……但我不知道你将使用什么浏览器,所以我的例子使用了 YUI 框架所以我的代码不会被浏览器规范化弄得乱七八糟。
Try saving the following as an HTML document. Then when you open it in your browser, try "do_stuff arg1 George 9 howdy" or "greet Navweb". I hope you can figure out what's going on by inspecting the code. I'm afraid I didn't have any time to put proper comments in.
尝试将以下内容另存为 HTML 文档。然后,当您在浏览器中打开它时,请尝试“do_stuff arg1 George 9 howdy”或“greet Navweb”。我希望你可以通过检查代码来弄清楚发生了什么。恐怕我没有时间发表适当的评论。
<!doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=Edge"/>
<title>Console</title>
<link rel="stylesheet" type="text/css" href="http://yui.yahooapis.com/combo?3.5.0/build/cssreset/cssreset-min.css"/>
<style type="text/css">
html {
background-color: #000;
}
body {
font-family: "Lucida Console";
font-size: 13px;
color: #0f0;
}
#in {
display: block;
position: fixed;
left: 0;
bottom: 0;
width: 100%;
padding: 8px;
border-color: #fff;
border-width: 1px 0 0 0;
background-color: #000;
color: #0f0;
}
</style>
</head>
<body>
<div id="out"></div>
<input id="in" tabindex="0"/>
</body>
<script type="text/javascript" src="http://yui.yahooapis.com/combo?3.5.0/build/yui/yui-min.js"></script>
<script type="text/javascript">
YUI().use("node", function(Y) {
var COMMANDS = [
{
name: "do_stuff",
handler: doStuff
},
{
name: "greet",
handler: function(args) {
outputToConsole("Hello " + args[0] + ", welcome to Console.");
}
}
];
function processCommand() {
var inField = Y.one("#in");
var input = inField.get("value");
var parts = input.replace(/\s+/g, " ").split(" ");
var command = parts[0];
var args = parts.length > 1 ? parts.slice(1, parts.length) : [];
inField.set("value", "");
for (var i = 0; i < COMMANDS.length; i++) {
if (command === COMMANDS[i].name) {
COMMANDS[i].handler(args);
return;
}
}
outputToConsole("Unsupported Command: " + command);
}
function doStuff(args) {
outputToConsole("I'll just echo the args: " + args);
}
function outputToConsole(text) {
var p = Y.Node.create("<p>" + text + "</p>");
Y.one("#out").append(p);
p.scrollIntoView();
}
Y.on("domready", function(e) {
Y.one("body").setStyle("paddingBottom", Y.one("#in").get("offsetHeight"));
Y.one("#in").on("keydown", function(e) {
if (e.charCode === 13) {
processCommand();
}
});
});
});
</script>
</html>
回答by moonshadow
The easiest, most compatible way is to use one of the many Javascript terminal emulator libraries such as termlib, jqueryterminaletc
最简单、最兼容的方法是使用众多 Javascript 终端模拟器库之一,例如termlib、jqueryterminal等