From e40fcbb7a70e3fedf8d08949a63c487a5d3fb300 Mon Sep 17 00:00:00 2001 From: Phred Date: Thu, 20 Nov 2014 16:47:47 -0600 Subject: [PATCH] removed repitious code --- client.js | 46 +++++++++++++++------------------------------- 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/client.js b/client.js index b07912b..7d0156b 100644 --- a/client.js +++ b/client.js @@ -11,6 +11,7 @@ return x.toLocaleString(); } + var functionApply = Function.prototype.apply; //debounce used to throttle sending to the server var debounce = function(func, wait, immediate) { var result; @@ -53,37 +54,20 @@ sendBuffer = []; }, 1000); //ancient browsers may lack a console - window.console = window.console || {}; - //patch console log, saving the original - var originalConsoleLog = console.log || function(){}; - console.log = function() { - if (arguments.length) { - originalConsoleLog.apply(console, arguments); - enqueue(arguments, 'log'); - } - }; - console.log.on = true; - //patch console warn, saving the original - var originalConsoleWarn = console.warn || function(){}; - console.warn = function() { - if (arguments.length) { - originalConsoleWarn.apply(console, arguments); - enqueue(arguments, 'warn'); - } - }; - console.warn.on = true; - //patch console error, same trick - var originalConsoleError = console.error || function(){}; - console.error = function() { - if (arguments.length) { - originalConsoleError.apply(console, arguments); - enqueue(arguments, 'error'); - } - }; - console.error.on = true; - //now, this is a different trick, monkey patch Error - var originalError = Error; - Error = function() { + var console = window.console || (window.console = {}); + var noop = function() {}; + ['error', 'log', 'warn'].forEach(function(name) { + var method = name in console && functionApply.bind(console[name], console) || noop; + + (console[name] = function() { + if (arguments.length) { + method(arguments); + enqueue(arguments, name); + } + }).on = true; + }); + + window.Error = function() { try { enbarf(); } catch(e) {