2

I am trying to catch every XHR Request send from Gmail and add a classification lable in the body of the mail. I am not sending new XHR requests. My code is working like filter for all XHR requests by overriding XMLHttpRequest's open/send function. I am working on this issue from last week.Please help me.

function sendEmail() {
    //alert("In sendEmail()");
    var overrideMethods = function() {
        //alert("In overrideMethods()");

        window.XMLHttpRequest.prototype._open = XMLHttpRequest.prototype.open;

        XMLHttpRequest.prototype.open = function (method, url, async, user, password) {
            this.openParams = {
                url: url
            };
            return window.XMLHttpRequest.prototype._open.apply(this, arguments);
        };

        window.XMLHttpRequest.prototype._send = XMLHttpRequest.prototype.send;

        window.XMLHttpRequest.prototype.send = function send() {
            var defered = false;
            var searchPattern = /(&selectedLable=|^selectedLable=)(.*?)&/;

            alert("In send()");
            if (typeof arguments[0] === "string" && arguments.length === 1) {
                var str = arguments[0];
                //alert("Inside of first if");
                //alert("str : " + str);
                if (this.openParams.url.match(/&act\=sm/) && str.match(/&bcc\=/) && str.match(searchPattern)) {
                    defered = true;

                    var sendData = (str.match(searchPattern) && str.match(searchPattern)[2]);
                    var tag = JSON.parse(decodeURIComponent(sendData)).tag;

                    alert("tag : " + tag);
                    /* Modify the POST url to reflect the tag */
                    str = str.replace(searchPattern, "");
                    str = str.replace(/&subject=/, "&subject=" + tag + ": ");
                    str = str.replace(/&body\=/, "&body=<br>" + tag.toLowerCase() + "<br>");

                    /* Capitalize the tag. */
                    arguments[0] = str + "&acn=!" + tag.charAt(0).toUpperCase() + tag.slice(1).toLowerCase();

                    window.XMLHttpRequest.prototype._send.apply(this, arguments);
                }
            } 

            if (!defered) { 
                window.XMLHttpRequest.prototype._send.apply(this, arguments);
            }
        };
    }
    window.location.href = 'javascript: (' + overrideMethods.toString().replace(/(\n|\ {2,})/gm, '') + ')();';
}

sendEmail();

I found this link on stack overflow : Overriding XMLHttpRequest's send method

Please note my method is working fine for native chrome extension, but its not working for crossrider extension.

Community
  • 1
  • 1
Mahesh
  • 351
  • 4
  • 7
  • 1
    So, **what** does not work? – Bergi Jul 02 '14 at 13:43
  • `window.location.href = 'javascript: (' + …` looks very scary – Bergi Jul 02 '14 at 13:44
  • @bergi Whenever gmail sends any xhr request, the **send** method overridden by me is not invoked. It calls send method only once. – Mahesh Jul 03 '14 at 05:15
  • I am adding javascript files with extension using the API provided by Crossrider by using appAPI.dom.addRemoteJS () method. – Mahesh Jul 03 '14 at 05:27
  • When I enter XMLHttpRequest.prototype.(send/open) on console, It shows me code of overridden method. I think issue is at below statement. window.location.href = 'javascript: (' + overrideMethods.toString().replace(/(\n|\ {2,})/gm, '') + ')();'; – Mahesh Jul 03 '14 at 05:37

0 Answers0