22

Is there a way to programmatically get input from the Javascript Console of Google Chrome, similar to readline() in Firefox?

Community
  • 1
  • 1
Daniel Duan
  • 2,493
  • 4
  • 21
  • 24
  • You can type the code that you want to evaluate in the console, and it will be immediately evaluated. For example, try typing `alert("Hello World!");` into the console. This isn't a complete answer to your question, but it's a good place to start. – Anderson Green Aug 27 '12 at 16:43

8 Answers8

6

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.

Marcin Wasilewski
  • 685
  • 1
  • 10
  • 26
5

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")

hichris123
  • 10,145
  • 15
  • 56
  • 70
Vedaant Arya
  • 475
  • 6
  • 18
  • 1
    I'm so silly haven't realized on my own how cool `command("user's command here")` is. Thank you for the direction! <3 – Brian Cannard Mar 29 '21 at 11:42
4

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.

var tempStore = [];
var oldLog = console.log;

console.log = function() {
    tempStore.push(arguments);
    oldLog.apply(console, arguments);
}
hichris123
  • 10,145
  • 15
  • 56
  • 70
1

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.

0

Sorry, doesn't work on Chrome JS Console, just works on the repl from repl.it

Example from repl.it:

console.log("Enter your name:");
console.read(function(name) {
  console.log('Your name is ' + name + '.');
});
sanrodari
  • 1,602
  • 2
  • 13
  • 23
0

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

0

The better you can do is use:

myVar = prompt('Which value do your want?')
vimuth
  • 5,064
  • 33
  • 79
  • 116
0

In a console-based environment (such as the JavaScript console in a browser's developer tools or a Node.js environment), you can use the readline module to handle user input.

Here's an example of using the readline module to read user input in a Node.js environment:

const readline = require("readline");

const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

rl.question("Please enter your name: ", (name) => {
  console.log(`Hello, ${name}!`);
  rl.close();
});

In this code, the readline module is used to create an interface for reading input. The rl.question() function prompts the user with the question "Please enter your name: ". The user's response is then captured and passed as an argument to the callback function (name) => {...}. Inside the callback, you can process the user's input, perform any necessary operations, and provide the desired output.

Once you have finished processing the user's input, it's important to call rl.close() to close the readline interface and allow the program to exit gracefully.

Keep in mind that this specific example uses Node.js, but in different environments or platforms, the way you handle user input may vary.

Anas
  • 1
  • 1