Greasy Fork is available in English.

Discussions » Development

Detect XHR request

§
Posted: 17-01-2016
Edited: 17-01-2016

Detect XHR request

Hey, I'd like to fire a function when the site does a XHR request.
The function I want to call uses GM_setValue and GM_getValue, so that makes it also more difficult.
I also tried the function below, but it only works when using it in the javascript console. When I use it with a userscript it doesn't work anymore...
I've already tried waitForKeyElements, but that doesn't seem to work for me.
And setTimeout and setInterval is not really an option.
Please help. Thanks in advance!

var oldXHR = window.XMLHttpRequest;
function newXHR() {
var realXHR = new oldXHR();
realXHR.addEventListener("readystatechange", function() {
if(realXHR.readyState==4 && realXHR.status==200){
afterAjaxComplete() //run your code here
}
}, false);
return realXHR;
}
window.XMLHttpRequest = newXHR;

Post reply

Sign in to post a reply.