2015-04-10 10:51:00 +03:00
|
|
|
"use strict";
|
2011-06-09 10:45:09 +04:00
|
|
|
if (!Date.prototype.toISOString) {
|
|
|
|
Date.prototype.toISOString = function () {
|
|
|
|
function pad(n) { return n < 10 ? '0' + n : n; }
|
|
|
|
function ms(n) { return n < 10 ? '00'+ n : n < 100 ? '0' + n : n }
|
|
|
|
return this.getFullYear() + '-' +
|
|
|
|
pad(this.getMonth() + 1) + '-' +
|
|
|
|
pad(this.getDate()) + 'T' +
|
|
|
|
pad(this.getHours()) + ':' +
|
|
|
|
pad(this.getMinutes()) + ':' +
|
|
|
|
pad(this.getSeconds()) + '.' +
|
|
|
|
ms(this.getMilliseconds()) + 'Z';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function createHAR(address, title, startTime, resources)
|
|
|
|
{
|
|
|
|
var entries = [];
|
|
|
|
|
|
|
|
resources.forEach(function (resource) {
|
|
|
|
var request = resource.request,
|
|
|
|
startReply = resource.startReply,
|
|
|
|
endReply = resource.endReply;
|
|
|
|
|
|
|
|
if (!request || !startReply || !endReply) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2013-03-20 00:51:54 +04:00
|
|
|
// Exclude Data URI from HAR file because
|
|
|
|
// they aren't included in specification
|
|
|
|
if (request.url.match(/(^data:image\/.*)/i)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-09 10:45:09 +04:00
|
|
|
entries.push({
|
|
|
|
startedDateTime: request.time.toISOString(),
|
|
|
|
time: endReply.time - request.time,
|
|
|
|
request: {
|
|
|
|
method: request.method,
|
|
|
|
url: request.url,
|
|
|
|
httpVersion: "HTTP/1.1",
|
|
|
|
cookies: [],
|
|
|
|
headers: request.headers,
|
|
|
|
queryString: [],
|
|
|
|
headersSize: -1,
|
|
|
|
bodySize: -1
|
|
|
|
},
|
|
|
|
response: {
|
|
|
|
status: endReply.status,
|
|
|
|
statusText: endReply.statusText,
|
|
|
|
httpVersion: "HTTP/1.1",
|
|
|
|
cookies: [],
|
|
|
|
headers: endReply.headers,
|
|
|
|
redirectURL: "",
|
|
|
|
headersSize: -1,
|
|
|
|
bodySize: startReply.bodySize,
|
|
|
|
content: {
|
|
|
|
size: startReply.bodySize,
|
|
|
|
mimeType: endReply.contentType
|
|
|
|
}
|
|
|
|
},
|
|
|
|
cache: {},
|
|
|
|
timings: {
|
|
|
|
blocked: 0,
|
|
|
|
dns: -1,
|
|
|
|
connect: -1,
|
|
|
|
send: 0,
|
|
|
|
wait: startReply.time - request.time,
|
|
|
|
receive: endReply.time - startReply.time,
|
|
|
|
ssl: -1
|
2012-08-28 20:16:25 +04:00
|
|
|
},
|
|
|
|
pageref: address
|
2011-06-09 10:45:09 +04:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
return {
|
|
|
|
log: {
|
|
|
|
version: '1.2',
|
|
|
|
creator: {
|
|
|
|
name: "PhantomJS",
|
|
|
|
version: phantom.version.major + '.' + phantom.version.minor +
|
|
|
|
'.' + phantom.version.patch
|
|
|
|
},
|
|
|
|
pages: [{
|
2011-06-13 19:11:40 +04:00
|
|
|
startedDateTime: startTime.toISOString(),
|
2011-06-09 10:45:09 +04:00
|
|
|
id: address,
|
|
|
|
title: title,
|
2012-08-28 20:16:25 +04:00
|
|
|
pageTimings: {
|
|
|
|
onLoad: page.endTime - page.startTime
|
|
|
|
}
|
2011-06-09 10:45:09 +04:00
|
|
|
}],
|
|
|
|
entries: entries
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2012-01-14 19:13:26 +04:00
|
|
|
var page = require('webpage').create(),
|
|
|
|
system = require('system');
|
2011-06-07 10:56:23 +04:00
|
|
|
|
2012-01-14 19:13:26 +04:00
|
|
|
if (system.args.length === 1) {
|
2012-12-07 00:29:37 +04:00
|
|
|
console.log('Usage: netsniff.js <some URL>');
|
2012-05-16 11:37:26 +04:00
|
|
|
phantom.exit(1);
|
2011-06-07 10:56:23 +04:00
|
|
|
} else {
|
2011-06-09 10:45:09 +04:00
|
|
|
|
2012-01-14 19:13:26 +04:00
|
|
|
page.address = system.args[1];
|
2011-06-09 10:45:09 +04:00
|
|
|
page.resources = [];
|
2011-06-07 10:56:23 +04:00
|
|
|
|
|
|
|
page.onLoadStarted = function () {
|
2011-06-09 10:45:09 +04:00
|
|
|
page.startTime = new Date();
|
2011-06-07 10:56:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
page.onResourceRequested = function (req) {
|
2011-06-09 10:45:09 +04:00
|
|
|
page.resources[req.id] = {
|
|
|
|
request: req,
|
|
|
|
startReply: null,
|
|
|
|
endReply: null
|
|
|
|
};
|
2011-06-08 12:01:30 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
page.onResourceReceived = function (res) {
|
2011-06-09 10:45:09 +04:00
|
|
|
if (res.stage === 'start') {
|
|
|
|
page.resources[res.id].startReply = res;
|
|
|
|
}
|
|
|
|
if (res.stage === 'end') {
|
|
|
|
page.resources[res.id].endReply = res;
|
|
|
|
}
|
2011-06-07 10:56:23 +04:00
|
|
|
};
|
|
|
|
|
2011-06-09 10:45:09 +04:00
|
|
|
page.open(page.address, function (status) {
|
|
|
|
var har;
|
2011-06-07 10:56:23 +04:00
|
|
|
if (status !== 'success') {
|
|
|
|
console.log('FAIL to load the address');
|
2013-05-18 18:39:49 +04:00
|
|
|
phantom.exit(1);
|
2011-06-07 10:56:23 +04:00
|
|
|
} else {
|
2012-08-28 20:16:25 +04:00
|
|
|
page.endTime = new Date();
|
2011-06-09 10:45:09 +04:00
|
|
|
page.title = page.evaluate(function () {
|
|
|
|
return document.title;
|
|
|
|
});
|
|
|
|
har = createHAR(page.address, page.title, page.startTime, page.resources);
|
|
|
|
console.log(JSON.stringify(har, undefined, 4));
|
2013-05-18 18:39:49 +04:00
|
|
|
phantom.exit();
|
2011-06-07 10:56:23 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|