| 1 | "use strict"; |
| 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'; |
| 13 | } |
| 14 | } |
| 15 | |
| 16 | function createHAR(address, title, startTime, resources) |
| 17 | { |
| 18 | var entries = []; |
| 19 | |
| 20 | resources.forEach(function (resource) { |
| 21 | var request = resource.request, |
| 22 | startReply = resource.startReply, |
| 23 | endReply = resource.endReply; |
| 24 | |
| 25 | if (!request || !startReply || !endReply) { |
| 26 | return; |
| 27 | } |
| 28 | |
| 29 | // Exclude Data URI from HAR file because |
| 30 | // they aren't included in specification |
| 31 | if (request.url.match(/(^data:image\/.*)/i)) { |
| 32 | return; |
| 33 | } |
| 34 | |
| 35 | entries.push({ |
| 36 | startedDateTime: request.time.toISOString(), |
| 37 | time: endReply.time - request.time, |
| 38 | request: { |
| 39 | method: request.method, |
| 40 | url: request.url, |
| 41 | httpVersion: "HTTP/1.1", |
| 42 | cookies: [], |
| 43 | headers: request.headers, |
| 44 | queryString: [], |
| 45 | headersSize: -1, |
| 46 | bodySize: -1 |
| 47 | }, |
| 48 | response: { |
| 49 | status: endReply.status, |
| 50 | statusText: endReply.statusText, |
| 51 | httpVersion: "HTTP/1.1", |
| 52 | cookies: [], |
| 53 | headers: endReply.headers, |
| 54 | redirectURL: "", |
| 55 | headersSize: -1, |
| 56 | bodySize: startReply.bodySize, |
| 57 | content: { |
| 58 | size: startReply.bodySize, |
| 59 | mimeType: endReply.contentType |
| 60 | } |
| 61 | }, |
| 62 | cache: {}, |
| 63 | timings: { |
| 64 | blocked: 0, |
| 65 | dns: -1, |
| 66 | connect: -1, |
| 67 | send: 0, |
| 68 | wait: startReply.time - request.time, |
| 69 | receive: endReply.time - startReply.time, |
| 70 | ssl: -1 |
| 71 | }, |
| 72 | pageref: address |
| 73 | }); |
| 74 | }); |
| 75 | |
| 76 | return { |
| 77 | log: { |
| 78 | version: '1.2', |
| 79 | creator: { |
| 80 | name: "PhantomJS", |
| 81 | version: phantom.version.major + '.' + phantom.version.minor + |
| 82 | '.' + phantom.version.patch |
| 83 | }, |
| 84 | pages: [{ |
| 85 | startedDateTime: startTime.toISOString(), |
| 86 | id: address, |
| 87 | title: title, |
| 88 | pageTimings: { |
| 89 | onLoad: page.endTime - page.startTime |
| 90 | } |
| 91 | }], |
| 92 | entries: entries |
| 93 | } |
| 94 | }; |
| 95 | } |
| 96 | |
| 97 | var page = require('webpage').create(), |
| 98 | system = require('system'); |
| 99 | |
| 100 | if (system.args.length === 1) { |
| 101 | console.log('Usage: netsniff.js <some URL>'); |
| 102 | phantom.exit(1); |
| 103 | } else { |
| 104 | |
| 105 | page.address = system.args[1]; |
| 106 | page.resources = []; |
| 107 | |
| 108 | page.onLoadStarted = function () { |
| 109 | page.startTime = new Date(); |
| 110 | }; |
| 111 | |
| 112 | page.onResourceRequested = function (req) { |
| 113 | page.resources[req.id] = { |
| 114 | request: req, |
| 115 | startReply: null, |
| 116 | endReply: null |
| 117 | }; |
| 118 | }; |
| 119 | |
| 120 | page.onResourceReceived = function (res) { |
| 121 | if (res.stage === 'start') { |
| 122 | page.resources[res.id].startReply = res; |
| 123 | } |
| 124 | if (res.stage === 'end') { |
| 125 | page.resources[res.id].endReply = res; |
| 126 | } |
| 127 | }; |
| 128 | |
| 129 | page.open(page.address, function (status) { |
| 130 | var har; |
| 131 | if (status !== 'success') { |
| 132 | console.log('FAIL to load the address'); |
| 133 | phantom.exit(1); |
| 134 | } else { |
| 135 | page.endTime = new Date(); |
| 136 | page.title = page.evaluate(function () { |
| 137 | return document.title; |
| 138 | }); |
| 139 | har = createHAR(page.address, page.title, page.startTime, page.resources); |
| 140 | console.log(JSON.stringify(har, undefined, 4)); |
| 141 | phantom.exit(); |
| 142 | } |
| 143 | }); |
| 144 | } |