简体   繁体   English

从console.log获取输入

[英]Getting the input from the console.log

How can I get the input from the console.log while using pure javascript? 使用纯JavaScript时,如何从console.log获取输入?

For example I wrote "hello world" in the console - I would like to add an event listener which will check this input. 例如,我在控制台中写了“ hello world”-我想添加一个事件侦听器来检查此输入。

How should I do it? 我该怎么办?

Rather than using an event listener, proxy the console itself: 代替使用事件侦听器,代理控制台本身:

interceptConsoleLog(fn) {
  const realLog = window.console.log;
  window.console.log = (function proxyLog() {
    // Do whatever you want the code to do, here.
    fn(...arguments); 

    // And then you fall through to the original log operation.
    realLog(...arguments)
  }).bind(console);
}

And then call it with your own handling function: 然后使用您自己的处理函数调用它:

function doWhatever() {
  // ...
}

interceptConsoleLog(doWhatever);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM