24

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

1
  • 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. Commented Aug 27, 2012 at 16:43

7 Answers 7

8

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

I'm so silly haven't realized on my own how cool command("user's command here") is. Thank you for the direction! <3
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);
}

Comments

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 + '.');
});

Comments

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

Comments

0

The better you can do is use:

myVar = prompt('Which value do your want?')

Comments

0

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:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.