如何从 Chrome 的 Javascript 控制台获取输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8299356/
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
How to get input from Chrome's Javascript console?
提问by Daniel Duan
Is there a way to programmatically get input from the Javascript Console of Google Chrome, similar to readline()
in Firefox?
有没有办法以编程方式从 Google Chrome 的 Javascript 控制台获取输入,类似于readline()
在 Firefox 中?
回答by Marcin Wasilewski
A tricky way to do this is assigning a getter to a property of a window object
一个棘手的方法是为 window 对象的属性分配一个 getter
Object.defineProperty(window, 'customCommand', {
get: function() {
console.log("hey");
return "hey";
}
});
So when you type "customCommand" (without parenthesis) it will print your console.log text to the console while the console is "getting" the variable.
因此,当您键入“customCommand”(不带括号)时,它会在控制台“获取”变量时将您的 console.log 文本打印到控制台。
You will still have to return something though, and I'm not sure how you could change the order so that the value is returned first and the text in the console appears second. It's definitely possible though, I've seen this happen.
尽管如此,您仍然必须返回一些内容,我不确定您如何更改顺序,以便首先返回值,然后控制台中的文本显示第二。不过这绝对有可能,我已经看到这种情况发生了。
回答by Vedaant Arya
This is an indirect method of taking inputs:
这是一种接受输入的间接方法:
Declare a function in JavaScript:
在 JavaScript 中声明一个函数:
function your_command_here() {
//code
}
As Chrome's console basically provides methods for communicating with the page's contents, like JavaScript variables, functions, etc., so declaring a function as a receivable command can be an option.
由于 Chrome 的控制台基本上提供了与页面内容通信的方法,例如 JavaScript 变量、函数等,因此可以选择将函数声明为可接收命令。
In the console, for providing input, the user shall type:
your_command_here()
在控制台中,为了提供输入,用户应键入:
your_command_here()
Another workaround is:
Declare a function:
另一种解决方法是:
声明一个函数:
function command(var cmnd) {
switch(cmnd) {
case "command1":
//code
break;
}
}
So the user can (more conveniently) type:
command("user's command here")
所以用户可以(更方便地)输入:
command("user's command here")
回答by Mehar Rahim
Here is a solution to input from the console. Try this out!!
这是从控制台输入的解决方案。试试这个!!
process.stdin.resume();
process.stdin.setEncoding('ascii');
var stdInput = "";
var stdInputArr = "";
var index = 0;
process.stdin.on('data', function (data) {
stdInput += data;
});
process.stdin.on('end', function () {
stdInputArr = stdInput.split("\n");
main();
});
// Reads complete line from STDIN
function readLine() {
return stdInputArr[index++];
}
//call this function in the main function
回答by curlyblueeagle
You might need to incorporate jsh (Javascript Shell) in your environment if you are working with console IO. See http://code.google.com/p/jsh/for the how-to. Hope this helps.
如果您使用控制台 IO,您可能需要在您的环境中加入 jsh(Javascript Shell)。有关操作方法,请参阅http://code.google.com/p/jsh/。希望这可以帮助。
回答by sanrodari
Sorry, doesn't work on Chrome JS Console, just works on the repl from repl.it
抱歉,不适用于 Chrome JS 控制台,仅适用于 repl.it 的 repl
Example from repl.it:
来自 repl.it 的示例:
console.log("Enter your name:");
console.read(function(name) {
console.log('Your name is ' + name + '.');
});
回答by sanketpatel299
We can do is hook the console.log so whenever it logs something we can access, otherwise there is no such direct method as like in firefox which does this possible for us in a simple single line code.
我们可以做的是钩住console.log,所以每当它记录我们可以访问的东西时,否则没有像firefox这样的直接方法,它可以通过简单的单行代码为我们做到这一点。
var tempStore = [];
var oldLog = console.log;
console.log = function() {
tempStore.push(arguments);
oldLog.apply(console, arguments);
}