0

I want to make browser extension for Firefox that detect the ajax code of of website that load the hidden page and redirect to new page ,like if user visit index.php where ajax load the two pages one is hiddenpage.php and redirect to new.php . Is there any other solution to detect this ajax at client side.

if (xmlhttp.readyState==4 && xmlhttp.status==200)
                {
                    //document.getElementById("myDiv").innerHTML="";
                }
            }
            xmlhttp.open("GET","hidden.php",true);
            xmlhttp.send();
        }

HTML

<a href="new.php" onclick="function();">click here</a>
TylerH
  • 20,799
  • 66
  • 75
  • 101

2 Answers2

0

You can modify XMLHttpRequest's prototype in an userscript.

/* Save the old method somewhere, it may be useful if you want to allow some AJAX */
XMLHttpRequest.prototype._send = XMLHttpRequest.prototype.send;

/* Override the previous method to define yours */
XMLHttpRequest.prototype.send  = function () {

    /* Do stuff here */
    alert(1);

    /* Use this line if you want to continue the AJAX request */
    XMLHttpRequest.prototype._send.apply(this, arguments);
}
Benoit Esnard
  • 2,017
  • 2
  • 24
  • 32
  • Thank you @Benoit Esnard, may be I could not convey my doubt properly. actually the above AJAX code is server side code and I can not modify this code. I am only interested to detect this AJAX pattern at client side from my browser if I load this index.php. – user3283549 Mar 11 '15 at 19:17
0
document.addEventListener('DOMContentLoaded', function() {
    getCurrentTabUrl(function(url) {
        fetchData(url);
    });
});

function fetchData(url)
{
    var xhr = new XMLHttpRequest();
    xhr.open("GET", url, true);
    xhr.onreadystatechange=function()
    {
        if (xhr.readyState==4 && xhr.status==200)
        {
            var data = xhr.responseText;
            var  index = data.indexOf('XMLHttpRequest');
            if(index != -1){
                document.getElementById("status").innerHTML = "The page contains AJAX requests";
            }else{
                document.getElementById("status").innerHTML = "Page doesn't contains AJAX";
            }
            //document.getElementById("status").innerHTML = data;
        }
    }
    //xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    //xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
    //xhr.setRequestHeader("Access-Control-Request-Method", "POST");
    xhr.send();
}

function getCurrentTabUrl(callback) {
    var queryInfo = {
    active: true,
    currentWindow: true
    };
    chrome.tabs.query(queryInfo, function(tabs) {
        var tab = tabs[0];
        var url = tab.url;
        console.assert(typeof url == 'string', 'tab.url should be a string');
        callback(url);
    });
}
just go through this code you will get the better help
Nirav Jani
  • 26
  • 2