Last active
August 15, 2024 09:58
-
-
Save joelcardinal/955a41e5979c4134f5575e1d3f0ddf48 to your computer and use it in GitHub Desktop.
Extend XMLHttpRequest to get info of all XHR requests
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
(function() { | |
// Extend XMLHttpRequest to get info of all XHR requests | |
// TODO: Add Fetch extension | |
// https://stackoverflow.com/questions/5202296/add-a-hook-to-all-ajax-requests-on-a-page | |
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress | |
// https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest | |
// https://developer.mozilla.org/en-US/docs/Web/Events/load | |
var origOpen = XMLHttpRequest.prototype.open; | |
XMLHttpRequest.prototype.open = function() { | |
console.log('request open'); | |
console.log(arguments); | |
console.log(this); | |
/* | |
this.addEventListener('loadstart', function() { | |
console.log('request loadstart'); | |
console.log(this); | |
}); | |
this.addEventListener('load', function() { | |
console.log('request load'); | |
console.log(this); | |
}); | |
*/ | |
this.addEventListener('loadend', function() { | |
// if status 2x and responseText... | |
console.log('request loadend'); | |
console.log(this); | |
}); | |
/* | |
this.addEventListener('progress', function() { | |
console.log('request progress'); | |
console.log(this); | |
}); | |
this.addEventListener('readystatechange', function() { | |
console.log('request readystatechange'); | |
console.log(this); | |
}); | |
*/ | |
this.addEventListener('abort', function() { | |
console.log('request abort'); | |
console.log(this); | |
}); | |
this.addEventListener('error', function() { | |
console.log('request error'); | |
console.log(this); | |
}); | |
this.addEventListener('timeout', function() { | |
console.log('request timeout'); | |
console.log(this); | |
}); | |
origOpen.apply(this, arguments); | |
}; | |
var oReq = new XMLHttpRequest(); | |
oReq.open("GET", "https://www.google.com"); | |
oReq.send(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment