繁体   English   中英

是否有允许您在 javascript 中监听控制台日志的功能?

[英]Is there a function that allows you to listen for console logs in javascript?

我试过用多种不同的措辞在多个网站上搜索但没有答案,这可能吗,不是吗,如果有,怎么做? 当 anythign 被记录到控制台时,我需要能够让代码运行并得到那个东西是什么。

您可以覆盖console.log的默认行为:

 var log = console.log; var logs = [] console.log = function(e) { log.apply(console, [].slice.call(arguments)); logs.push(e) //custom code }; console.log("Hello World!") console.log('Printed logs:', logs)

  1. 保存原始控制台

    var log = console.log;

  2. 猴补丁IE修改它在运行时,所有在运行时//使用日志实例和'apply()'方法应用到继承它//我们在日期为传递每次调用console.log()使用时, original ( log() )

    log.apply(console, [(new Date().toString())].concat([].slice.call(arguments)) ); };

  3. 现在每次我们调用 - log() - 我们都在调用原始 // 来测试它这样做

    log('test','this is the original console.log());

  4. 这是补丁版本

    console.log("This is the Monkey Patched console.log()!", "More text...");

暂无
暂无

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

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