1 if (!Date.prototype.toISOString) {
2 Date.prototype.toISOString = function () {
3 function pad(n) { return n < 10 ? '0' + n : n; }
4 function ms(n) { return n < 10 ? '00'+ n : n < 100 ? '0' + n : n }
5 return this.getFullYear() + '-' +
6 pad(this.getMonth() + 1) + '-' +
7 pad(this.getDate()) + 'T' +
8 pad(this.getHours()) + ':' +
9 pad(this.getMinutes()) + ':' +
10 pad(this.getSeconds()) + '.' +
11 ms(this.getMilliseconds()) + 'Z';
15 function createHAR(address, title, startTime, resources)
19 resources.forEach(function (resource) {
20 var request = resource.request,
21 startReply = resource.startReply,
22 endReply = resource.endReply;
24 if (!request || !startReply || !endReply) {
28 // Exclude Data URI from HAR file because
29 // they aren't included in specification
30 if (request.url.match(/(^data:image\/.*)/i)) {
35 startedDateTime: request.time.toISOString(),
36 time: endReply.time - request.time,
38 method: request.method,
40 httpVersion: "HTTP/1.1",
42 headers: request.headers,
48 status: endReply.status,
49 statusText: endReply.statusText,
50 httpVersion: "HTTP/1.1",
52 headers: endReply.headers,
55 bodySize: startReply.bodySize,
57 size: startReply.bodySize,
58 mimeType: endReply.contentType
67 wait: startReply.time - request.time,
68 receive: endReply.time - startReply.time,
80 version: phantom.version.major + '.' + phantom.version.minor +
81 '.' + phantom.version.patch
84 startedDateTime: startTime.toISOString(),
88 onLoad: page.endTime - page.startTime
96 var page = require('webpage').create(),
97 system = require('system');
99 if (system.args.length === 1) {
100 console.log('Usage: netsniff.js <some URL>');
104 page.address = system.args[1];
107 page.onLoadStarted = function () {
108 page.startTime = new Date();
111 page.onResourceRequested = function (req) {
112 page.resources[req.id] = {
119 page.onResourceReceived = function (res) {
120 if (res.stage === 'start') {
121 page.resources[res.id].startReply = res;
123 if (res.stage === 'end') {
124 page.resources[res.id].endReply = res;
128 page.open(page.address, function (status) {
130 if (status !== 'success') {
131 console.log('FAIL to load the address');
134 page.endTime = new Date();
135 page.title = page.evaluate(function () {
136 return document.title;
138 har = createHAR(page.address, page.title, page.startTime, page.resources);
139 console.log(JSON.stringify(har, undefined, 4));