如何从 Chrome 的 Javascript 控制台获取输入? [英] How to get input from Chrome's Javascript console?

查看:29
本文介绍了如何从 Chrome 的 Javascript 控制台获取输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法以编程方式从 Google Chrome 的 Javascript 控制台获取输入,类似于 readline() 在 Firefox 中?

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

推荐答案

一个棘手的方法是为窗口对象的属性分配一个 getter

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

因此,当您键入customCommand"(不带括号)时,它会在控制台获取"变量时将您的 console.log 文本打印到控制台.

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.

这篇关于如何从 Chrome 的 Javascript 控制台获取输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆