How to get input from Chrome's Javascript console?

前端 未结 6 736
情话喂你
情话喂你 2020-12-29 06:21

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

相关标签:
6条回答
  • 2020-12-29 07:04

    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 + '.');
    });
    
    0 讨论(0)
  • 2020-12-29 07:17

    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
    

    javascript node.js jquery consoleweb

    0 讨论(0)
  • 2020-12-29 07:20

    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.

    0 讨论(0)
  • 2020-12-29 07:23

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

    0 讨论(0)
  • 2020-12-29 07:25

    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);
    }
    
    0 讨论(0)
  • 2020-12-29 07:27

    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 讨论(0)
提交回复
热议问题