Last active
June 27, 2017 22:17
-
-
Save ConnerAiken/5965dae0f1b8e1e5799145e3d721e76a to your computer and use it in GitHub Desktop.
Hook into all AJAX calls
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
/* =================== jQuery ===================== */ | |
$(document).ajaxSend(function() { | |
alert("Triggered ajaxSend hook."); | |
}); | |
$('#clickBtn').click(function() { | |
$.getJSON("https://crossorigin.me/https://api.darksky.net/forecast/037352c0951701a563b624359ea6111f/42.3601,-71.0589", function(json) { | |
console.log("JSON Data:", json); | |
}); | |
}); | |
/* =================== Vanilla JS ===================== */ | |
var open = window.XMLHttpRequest.prototype.open, | |
send = window.XMLHttpRequest.prototype.send; | |
function sendReplacement(data) { | |
if(this.onreadystatechange) { | |
this._onreadystatechange = this.onreadystatechange; | |
} | |
this.onreadystatechange = onReadyStateChangeReplacement | |
alert("Triggered XHR hook"); | |
return send.apply(this, arguments); | |
} | |
function onReadyStateChangeReplacement() { | |
if(this._onreadystatechange) { | |
return this._onreadystatechange.apply(this, arguments); | |
} | |
} | |
window.XMLHttpRequest.prototype.send = sendReplacement; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment