diff --git a/client.js b/client.js index b07912b..c2ca195 100644 --- a/client.js +++ b/client.js @@ -2,26 +2,27 @@ * This is the flinger client library. */ ; -(function () { +(function(window, $) { window.flingerAdditionalClientData = function () { return ""; - } + }; window.flingerFormatter = function (x) { return x.toLocaleString(); - } + }; + var arrayMap = Array.prototype.map; //debounce used to throttle sending to the server var debounce = function(func, wait, immediate) { - var result; - var timeout = null; + var result, timeout; return function() { - var context = this, args = arguments; + var args = arguments; + var callNow = immediate && !timeout; + var context = this; var later = function() { timeout = null; if (!immediate) result = func.apply(context, args); }; - var callNow = immediate && !timeout; clearTimeout(timeout); timeout = setTimeout(later, wait); if (callNow) result = func.apply(context, args); @@ -33,18 +34,18 @@ //enqueue up for transmission var enqueue = function(logArguments, kind, stack) { if (!console[kind].on) return; - message = { - arguments: Array.prototype.slice.call(logArguments).map(function (x) { return flingerFormatter(x); }), + var message = { + arguments: arrayMap.call(logArguments, function (x) { return flingerFormatter(x); }), kind: kind, - stack: stack, + stack: stack }; message.user = flingerAdditionalClientData(message); sendBuffer.push(message); send(); - } + }; //send along to the server - var send = debounce(function(){ - jQuery.ajax({ + var send = debounce(function() { + $.ajax({ type: "POST", url: "/", contentType: 'application/flinger', @@ -53,7 +54,7 @@ sendBuffer = []; }, 1000); //ancient browsers may lack a console - window.console = window.console || {}; + var console = window.console || {}; //patch console log, saving the original var originalConsoleLog = console.log || function(){}; console.log = function() { @@ -82,8 +83,7 @@ }; console.error.on = true; //now, this is a different trick, monkey patch Error - var originalError = Error; - Error = function() { + window.Error = function() { try { enbarf(); } catch(e) { @@ -92,4 +92,4 @@ }; console.exception = Error; console.exception.on = true; -})(); +})(this, this.jQuery);