JavaScript Console Control

017th Dec 2014Development, , ,

javascript-console-log I’ve been working on a new project lately that I’m calling JScage. Essentially it’s a browser-based JavaScript/CoffeeScript, CSS/LESS/SASS, HTML IDE with a wide array of features. It’s been a great deal of fun, and I’ve picked up a few things…

I’m not really sure where I got the base code from, but I did a bit of modifying and need to do even more to further cover the full functionality of the console. So, without further ado, the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
function takeOverConsole() {
    var console = window.console
    if (!console) return
 
    function intercept(method) {
        var original = console[method];
        console[method] = function () {
            // do sneaky stuff
            var message;
            if (typeof (message) == 'object') {
                message = Array.prototype.slice.apply(arguments)[0];
                var vals = [];
                Object.keys(message).forEach(function (key) {
                    vals.push(key);
                    vals.push(message[key]);
                });
            } else {
                var strOut = Array.prototype.slice.apply(arguments).join(' ')
            }
            if (original.apply) {
                // Do this for normal browsers
                original.apply(console, arguments)
            } else {
                // Do this for IE
                var message = Array.prototype.slice.apply(arguments).join(' ')
                original(message)
            }
        }
    }
    var methods = ['log', 'warn', 'error']
    for (var i = 0; i < methods.length; i++)
    intercept(methods[i])
}
 
takeOverConsole();

No Comments Comments Feed

Add a Comment

Before you post, please prove you are sentient.

What is 2 multiplied by 9?