Skip to content

Instantly share code, notes, and snippets.

@cvazac
Last active September 12, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cvazac/bfdb71ecfe610656aaf3bca596a51e47 to your computer and use it in GitHub Desktop.
Save cvazac/bfdb71ecfe610656aaf3bca596a51e47 to your computer and use it in GitHub Desktop.
!(function(){
if (typeof navigator.sendBeacon !== 'function') return
if (/\[native code\]/.test(Function.prototype.toString.call(navigator.sendBeacon))) return
var orig_sendBeacon = navigator.sendBeacon
window.addEventListener('unload', function() {
navigator.sendBeacon = orig_sendBeacon
})
navigator.sendBeacon = function() {
var orig_open = XMLHttpRequest.prototype.open
XMLHttpRequest.prototype.open = function() {
if (arguments.length < 3)
return orig_open.apply(this, arguments)
var args = Array.prototype.slice.call(arguments)
args[2] = true
return orig_open.apply(this, args)
}
var returnValue, didThrow = false, e
try {
returnValue = orig_sendBeacon.apply(this, arguments)
} catch(_e) {
didThrow = true
e = _e
}
XMLHttpRequest.prototype.open = orig_open
if (didThrow) {
throw e
}
return returnValue
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment