How to get input from Chrome's Javascript console?

前端 未结 6 759
情话喂你
情话喂你 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: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.

提交回复
热议问题