Listening console.log

五迷三道 提交于 2020-01-09 10:25:07

问题


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


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/6455631/listening-console-log

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!