How to get input from Chrome's Javascript console?

前端 未结 6 762
情话喂你
情话喂你 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: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")

提交回复
热议问题