vol7ron
vol7ron

Reputation: 42109

Retrieve and Abort open XMLHttpRequest objects

Does the browser have some way to target open requests?

I have a plugin that calls some requests, which doesn't expose the XMLHttpRequest/jqXHR object; however, Firebug does show these requests' operations in the console.

Is there a way to reference the objects for open requests? The end goal is to abort existing one.

Upvotes: 1

Views: 311

Answers (1)

plalx
plalx

Reputation: 43718

I believe that a FF extension has more capabilities than the code running in the page's context and that's why it can detect requests. As far as I know there's no native way to retrieve an XMLHttpRequest object instance if you haven't kept any reference to it.

However, JavaScript is a flexible language and you can easily create an interceptor that would track all open calls.

Note: It would be preferable to hook in the plugin methods if possible.

E.g.

XMLHttpRequest.prototype.send = (function (nativeSendFn) {
    return function () {
        var sendHandler = this.constructor.onSend;
        nativeSendFn.apply(this, arguments);

        sendHandler && sendHandler(this, arguments);
    };
})(XMLHttpRequest.prototype.send);

XMLHttpRequest.onSend = function (xhr, args) {
    console.log('request ', xhr);
    console.log('arguments ', args);
};

var req = new XMLHttpRequest();

req.open('GET', 'http://www.stackoverflow.com', true);
req.send();

Upvotes: 1

Related Questions