Is there a way to programmatically get input from the Javascript Console of Google Chrome, similar to readline() in Firefox?
7 Answers
A tricky way to do this is assigning a getter to a property of a window object
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.
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.
Comments
This is an indirect method of taking inputs:
Declare a function in 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.
In the console, for providing input, the user shall type:
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")
1 Comment
command("user's command here") is. Thank you for the direction! <3We 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.
var tempStore = [];
var oldLog = console.log;
console.log = function() {
tempStore.push(arguments);
oldLog.apply(console, arguments);
}
Comments
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
Comments
This solution works, but the input syntax is not as simple as in NodeJS:
$`your value`
here is the code:
// declare global 'consoleInput()'
window.consoleInput = prompt => {
console.log(prompt);
return new Promise(resolve => window.$ = value =>
resolve(value.toString()));
};
// usage. do not forget 'await' keyword
const name = await consoleInput('What is your name?');
const age = await consoleInput('How old are you?');
console.log(`Your name is ${name} and you are ${age} years old.`);
Note: The syntax for connecting Javascript to html must contain the type="module" attribute:
<body>
<h1>Hello World</h1>
<script src="app.js" type="module"></script>
</body>
or, if you do not want to use type="module" attribute:
// usage
async function input() {
const name = await consoleInput('What is your name?');
const age = await consoleInput('How old are you?');
console.log(`Hello, ${name}! You are ${age} years old.`);
}
input();
usage in browser console:

alert("Hello World!");into the console. This isn't a complete answer to your question, but it's a good place to start.