Skip to content

Instantly share code, notes, and snippets.

@Muzietto
Last active December 15, 2017 13:11
Show Gist options
  • Save Muzietto/78d8f682094e42aa4cae95951291bf92 to your computer and use it in GitHub Desktop.
Save Muzietto/78d8f682094e42aa4cae95951291bf92 to your computer and use it in GitHub Desktop.
This is how Raygun manages to peek into window.onerror and dispatch the error to its servers
!function (
window,
document,
scriptString,
minifiedRaygunScript,
rg4jsString,
domScriptElement,
firstStriptTag,
windowOnError
) {
window.RaygunObject = rg4jsString,
window[rg4jsString] = window[rg4jsString] || function () {
(window[rg4jsString].o = window[rg4jsString].o || []).push(arguments);
},
domScriptElement = document.createElement(scriptString),
firstStriptTag = document.getElementsByTagName(scriptString)[0],
domScriptElement.async = 1,
domScriptElement.src = minifiedRaygunScript,
firstStriptTag.parentNode.insertBefore(domScriptElement, firstStriptTag),
// from MDN: window.onerror = function(message, sourceScript, lineno, colno, errorObj)
windowOnError = window.onerror,
window.onerror = function (msg, sourceScript, lineNumber, columnNumber, errorObj) {
windowOnError && windowOnError(msg, sourceScript, lineNumber, columnNumber, errorObj),
errorObj || (errorObj = new Error(msg)),
window[rg4jsString].q = window[rg4jsString].q || [],
window[rg4jsString].q.push({
e: errorObj
});
};
}(window, document, 'script', '//cdn.raygun.io/raygun4js/raygun.min.js', 'rg4js');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment