Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Intercept send mail AJAX request in Gmail

I'm trying to attach a callback to the "Send mail" ajax action in Gmail. I've been able to differentiate a Send mail action from other AJAX actions based on the request payload but have been unable to hook into the actual AJAX call.

Thus far, I've tried using overriding the XMLHttpRequest.open() method as detailed here. That hasn't worked. I've also tried overriding XMLHttpRequest.send(). Also failed.

Any thoughts? Much thanks in advance.

like image 711
Rui Jiang Avatar asked Jul 05 '11 14:07

Rui Jiang


1 Answers

Google's trick is that they send the request from inside an iframe which has it's own JavaScript environment. However, since it is loaded from the same origin as the parent, you can still easily manipulate it even from the browser console:

[].slice.apply(document.querySelectorAll('iframe')).forEach(function (iframe) {
    try {
        var xhrProto = iframe.contentWindow.XMLHttpRequest.prototype;
        var origOpen = xhrProto.open;
        xhrProto.open = function () {
            console.log('DO SOMETHING', arguments);
            return origOpen.apply(this, arguments);
        };
    } catch (e) {}
});

You might want to use a MutationObserver to detect newly added iframes reliably.

like image 167
Joel Avatar answered Sep 18 '22 17:09

Joel