How can I intercept XMLHttpRequests from a Greasemonkey script?

Scooby Doo picture Scooby Doo · Mar 10, 2009 · Viewed 37.2k times · Source

I would like to capture the contents of AJAX requests using Greasemonkey.

Does anybody know how to do this?

Answer

Sean Anderson picture Sean Anderson · Mar 27, 2015

The accepted answer is almost correct, but it could use a slight improvement:

(function(open) {
    XMLHttpRequest.prototype.open = function() {
        this.addEventListener("readystatechange", function() {
            console.log(this.readyState);
        }, false);
        open.apply(this, arguments);
    };
})(XMLHttpRequest.prototype.open);

Prefer using apply + arguments over call because then you don't have to explicitly know all the arguments being given to open which could change!