1 if not Date::toISOString
4 if n < 10 then '0' + n else n
6 if n < 10 then '00' + n else (if n < 100 then '0' + n else n)
8 pad(@getMonth() + 1) + '-' +
9 pad(@getDate()) + 'T' +
10 pad(@getHours()) + ':' +
11 pad(@getMinutes()) + ':' +
12 pad(@getSeconds()) + '.' +
13 ms(@getMilliseconds()) + 'Z'
15 createHAR = (address, title, startTime, resources) ->
18 resources.forEach (resource) ->
19 request = resource.request
20 startReply = resource.startReply
21 endReply = resource.endReply
23 if not request or not startReply or not endReply
27 startedDateTime: request.time.toISOString()
28 time: endReply.time - request.time
30 method: request.method
32 httpVersion: 'HTTP/1.1'
34 headers: request.headers
40 status: endReply.status
41 statusText: endReply.statusText
42 httpVersion: 'HTTP/1.1'
44 headers: endReply.headers
47 bodySize: startReply.bodySize
49 size: startReply.bodySize
50 mimeType: endReply.contentType
58 wait: startReply.time - request.time
59 receive: endReply.time - startReply.time
67 version: phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch
70 startedDateTime: startTime.toISOString()
74 onLoad: page.endTime - page.startTime
78 page = require('webpage').create()
79 system = require 'system'
81 if system.args.length is 1
82 console.log 'Usage: netsniff.coffee <some URL>'
85 page.address = system.args[1]
88 page.onLoadStarted = ->
89 page.startTime = new Date()
91 page.onResourceRequested = (req) ->
92 page.resources[req.id] =
97 page.onResourceReceived = (res) ->
98 if res.stage is 'start'
99 page.resources[res.id].startReply = res
100 if res.stage is 'end'
101 page.resources[res.id].endReply = res
103 page.open page.address, (status) ->
104 if status isnt 'success'
105 console.log 'FAIL to load the address'
108 page.endTime = new Date()
109 page.title = page.evaluate ->
112 har = createHAR page.address, page.title, page.startTime, page.resources
113 console.log JSON.stringify har, undefined, 4