开发者

Listening console.log

开发者 https://www.devze.com 2023-03-15 03:17 出处:网络
I want to set a listener for console.log() and do something with the message without preventing the default behaviour. So, 开发者_如何学JAVAthe console of the dev tools should get the message as well.

I want to set a listener for console.log() and do something with the message without preventing the default behaviour. So, 开发者_如何学JAVAthe console of the dev tools should get the message as well. Any ideas?


Never tried this in a webpage, but it work in a browser plugin (where javascripts rights are are not the same for security reasons).

You could definitively go for something like this :

(function(){

    var originallog = console.log;

    console.log = function(txt) {
        // Do really interesting stuff
        alert("I'm doing interesting stuff here !");

        originallog.apply(console, arguments);
    }

})();

The funny thing in javascript is that function are objects too :D


This is a small hack, but I'm not sure there is a better solution:

console._log_old = console.log
console.log = function(msg) {
    alert(msg);
    console._log_old(msg);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消