在Chrome中拦截对console.log的调用 [英] Intercept calls to console.log in Chrome

查看:1076
本文介绍了在Chrome中拦截对console.log的调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个脚本,我不能改变,使很多console.log调用。我想添加另一个层,并回应如果调用包含某些字符串。这在FF中工作,但在第4行的Chrome中引发了非法调用错误:

I have a script that I can't change that makes a lot of console.log calls. I want to add another layer and respond if the calls contain certain strings. This works in FF, but throws an "Illegal invocation" error in Chrome on the 4th line:

var oldConsole = {};
oldConsole.log = console.log;
console.log = function (arg) {
    oldConsole.log('MY CONSOLE!!');
    oldConsole.log(arg);
}

任何想法如何解决?我还试过克隆控制台...

Any ideas how to get around that? I also tried cloning the console...

推荐答案

您需要调用 console.log console 的上下文中:chrome:

You need to call console.log in the context of console for chrome:

(function () {
  var log = console.log;
  console.log = function () {
    log.call(this, 'My Console!!!');
    log.apply(this, Array.prototype.slice.call(arguments));
  };
}());

这篇关于在Chrome中拦截对console.log的调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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