cyx
cyx

Reputation: 103

How to collect instances of XMLHttpRequest?

I want to collect all instances of XMLHttpRequest

Here is my code:

var array = [];
var orig_XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
    array.push(this);
    return new orig_XMLHttpRequest();
}

var ajax_request = new XMLHttpRequest();

but because "return new orig_XMLHttpRequest();"

so array[0] is not I actually want (ajax_request), I think in array[0] is an empty object?

Because I will override XMLHttpRequest and it has to use 'new' keyword and then return, so I can't get the correct "this".....

Is there any way to get all instances of XMLHttpRequest?

thanks

Upvotes: 0

Views: 310

Answers (1)

Amit
Amit

Reputation: 46323

You need to push the created instance to your array:

var array = [];
var orig_XMLHttpRequest = XMLHttpRequest;
XMLHttpRequest = function() {
    var xhr = new orig_XMLHttpRequest();
    array.push(xhr);
    return xhr;
}

var ajax_request = new XMLHttpRequest();

Upvotes: 2

Related Questions