Listening console.log

前端 未结 2 1052
感动是毒
感动是毒 2020-12-09 05:55

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 g

相关标签:
2条回答
  • 2020-12-09 06:32

    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 讨论(0)
  • 2020-12-09 06:38

    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

    0 讨论(0)
提交回复
热议问题