Created
February 17, 2015 19:19
-
-
Save rebolyte/c0cd3c83b9fe9ade0d33 to your computer and use it in GitHub Desktop.
deconstructing errorception's snippet
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Original snippet: | |
// https://errorception.com/docs | |
var SCRIPT_EL = 'script'; | |
function (window, document, SCRIPT_EL, PROJ_ID) { | |
// Set a global variable called '_errs' to an array with our PROJ_ID in it | |
window._errs = [PROJ_ID]; | |
// Specify our method to be executed when 'onerror' fires. This gives us | |
// the ability to grab the arguments passed to onerror, stash them, and | |
// then call onerror as usual. | |
var myOnErrMethod = window.onerror; | |
window.onerror = function() { | |
// Reference the special 'arguments' param available in every JS func | |
var args = arguments; | |
// Add what's in 'arguments' to our error array | |
_errs.push(args); | |
// If myOnErrMethod exists (doesn't return null), run it in 'this' | |
// context, giving onError the arguments it was originally passed. | |
myOnErrMethod && myOnErrMethod.apply(this, args); | |
}; | |
// Add the reference to our tracking script as a <script> element before | |
// the first <script> already in the page. | |
var createErrReporterElement = function() { | |
var scriptEl = document.createElement(SCRIPT_EL); | |
var firstScriptInPage = document.getElementsByTagName(SCRIPT_EL)[0]; | |
scriptEl.src = "//beacon.errorception.com/" + PROJ_ID + ".js"; | |
scriptEl.async = !0; | |
firstScriptInPage.parentNode.insertBefore(scriptEl, firstScriptInPage); | |
}; | |
// Depending on whether 'addEventListener' is present (i.e., whether we're | |
// in old IE or not), use the appropriate method to attach our function | |
// to the 'onload' event. | |
window.addEventListener ? window.addEventListener("load", createErrReporterElement, !1) : window.attachEvent("onload", createErrReporterElement) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment