I took the intercept code from here and wrapped it in a PhantomJS script which injected it into the page I was scraping. Note that the page has to be loaded before injecting the XHTTP intercept.
Also, had to tell PhantomJS to intercept and print out messages printed to console.log.
I used the [functions] technique from Vijay's accepted answer here
For a more interesting live data feed try using http://flightaware.com/live/ instead of maps.google.com below, but be patient, it may take a minute or five to get an update.
Here's the partial (untested other than parse, sorry) PhantomJS script:
var page = new WebPage(), testindex = 0, loadInProgress = false;
page.onLoadStarted = function() {
loadInProgress = true;
console.log("load started");
};
page.onLoadFinished = function() {
loadInProgress = false;
console.log("load finished");
};
page.onConsoleMessage = function(msg) {
console.log(msg);
};
var steps = [
function() {
//Load Login Page
page.open("http://maps.google.com");
},
function() {
page.render('check.png'); // see what's happened.
page.evaluate(
function( x) {
//inject following code from https://gist.github.com/suprememoocow/2823600
// I've added console.log() calls along with onConsoleMessage above to see XHR responses.
(function(XHR) {
"use strict";
var stats = [];
var timeoutId = null;
var open = XHR.prototype.open;
var send = XHR.prototype.send;
XHR.prototype.open = function(method, url, async, user, pass) {
this._url = url;
open.call(this, method, url, async, user, pass);
};
XHR.prototype.send = function(data) {
var self = this;
var start;
var oldOnReadyStateChange;
var url = this._url;
function onReadyStateChange() {
if(self.readyState == 4 /* complete */) {
var time = new Date() - start;
stats.push({
url: url,
duration: time
});
console.log( "Request:" + data);
console.log( "Response:" + this.responseText );
if(!timeoutId) {
timeoutId = window.setTimeout(function() {
var xhr = new XHR();
xhr.noIntercept = true;
xhr.open("POST", "/clientAjaxStats", true);
xhr.setRequestHeader("Content-type","application/json");
xhr.send(JSON.stringify({ stats: stats } ));
timeoutId = null;
stats = [];
}, 2000);
}
}
if(oldOnReadyStateChange) {
oldOnReadyStateChange();
}
}
if(!this.noIntercept) {
start = new Date();
if(this.addEventListener) {
this.addEventListener("readystatechange", onReadyStateChange, false);
} else {
oldOnReadyStateChange = this.onreadystatechange;
this.onreadystatechange = onReadyStateChange;
}
}
send.call(this, data);
}
})(XMLHttpRequest);
},""
);
},
function() {
// try something else here. Add more steps as necessary
}
];
interval = setInterval(function() {
if (!loadInProgress && typeof steps[testindex] == "function") {
console.log("step " + (testindex + 1));
steps[testindex]();
testindex++;
}
if (typeof steps[testindex] != "function") {
// commented out to run until ctrl-c
//console.log("test complete!");
//phantom.exit();
}
}, 500);