2 if (!Date.prototype.toISOString) {
3 Date.prototype.toISOString = function () {
4 function pad(n) { return n < 10 ? '0' + n : n; }
5 function ms(n) { return n < 10 ? '00'+ n : n < 100 ? '0' + n : n }
6 return this.getFullYear() + '-' +
7 pad(this.getMonth() + 1) + '-' +
8 pad(this.getDate()) + 'T' +
9 pad(this.getHours()) + ':' +
10 pad(this.getMinutes()) + ':' +
11 pad(this.getSeconds()) + '.' +
12 ms(this.getMilliseconds()) + 'Z';
16 function createHAR(address, title, startTime, resources)
20 resources.forEach(function (resource) {
21 var request = resource.request,
22 startReply = resource.startReply,
23 endReply = resource.endReply;
25 if (!request || !startReply || !endReply) {
29 // Exclude Data URI from HAR file because
30 // they aren't included in specification
31 if (request.url.match(/(^data:image\/.*)/i)) {
36 startedDateTime: request.time.toISOString(),
37 time: endReply.time - request.time,
39 method: request.method,
41 httpVersion: "HTTP/1.1",
43 headers: request.headers,
49 status: endReply.status,
50 statusText: endReply.statusText,
51 httpVersion: "HTTP/1.1",
53 headers: endReply.headers,
56 bodySize: startReply.bodySize,
58 size: startReply.bodySize,
59 mimeType: endReply.contentType
68 wait: startReply.time - request.time,
69 receive: endReply.time - startReply.time,
81 version: phantom.version.major + '.' + phantom.version.minor +
82 '.' + phantom.version.patch
85 startedDateTime: startTime.toISOString(),
89 onLoad: page.endTime - page.startTime
97 var page = require('webpage').create(),
98 system = require('system');
100 if (system.args.length === 1) {
101 console.log('Usage: netsniff.js <some URL>');
105 page.address = system.args[1];
108 page.onLoadStarted = function () {
109 page.startTime = new Date();
112 page.onResourceRequested = function (req) {
113 page.resources[req.id] = {
120 page.onResourceReceived = function (res) {
121 if (res.stage === 'start') {
122 page.resources[res.id].startReply = res;
124 if (res.stage === 'end') {
125 page.resources[res.id].endReply = res;
129 page.open(page.address, function (status) {
131 if (status !== 'success') {
132 console.log('FAIL to load the address');
135 page.endTime = new Date();
136 page.title = page.evaluate(function () {
137 return document.title;
139 har = createHAR(page.address, page.title, page.startTime, page.resources);
140 console.log(JSON.stringify(har, undefined, 4));