netsniff.coffee 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. if not Date::toISOString
  2. Date::toISOString = ->
  3. pad = (n) ->
  4. if n < 10 then '0' + n else n
  5. ms = (n) ->
  6. if n < 10 then '00' + n else (if n < 100 then '0' + n else n)
  7. @getFullYear() + '-' +
  8. pad(@getMonth() + 1) + '-' +
  9. pad(@getDate()) + 'T' +
  10. pad(@getHours()) + ':' +
  11. pad(@getMinutes()) + ':' +
  12. pad(@getSeconds()) + '.' +
  13. ms(@getMilliseconds()) + 'Z'
  14. createHAR = (address, title, startTime, resources) ->
  15. entries = []
  16. resources.forEach (resource) ->
  17. request = resource.request
  18. startReply = resource.startReply
  19. endReply = resource.endReply
  20. if not request or not startReply or not endReply
  21. return
  22. entries.push
  23. startedDateTime: request.time.toISOString()
  24. time: endReply.time - request.time
  25. request:
  26. method: request.method
  27. url: request.url
  28. httpVersion: 'HTTP/1.1'
  29. cookies: []
  30. headers: request.headers
  31. queryString: []
  32. headersSize: -1
  33. bodySize: -1
  34. response:
  35. status: endReply.status
  36. statusText: endReply.statusText
  37. httpVersion: 'HTTP/1.1'
  38. cookies: []
  39. headers: endReply.headers
  40. redirectURL: ''
  41. headersSize: -1
  42. bodySize: startReply.bodySize
  43. content:
  44. size: startReply.bodySize
  45. mimeType: endReply.contentType
  46. cache: {}
  47. timings:
  48. blocked: 0
  49. dns: -1
  50. connect: -1
  51. send: 0
  52. wait: startReply.time - request.time
  53. receive: endReply.time - startReply.time
  54. ssl: -1
  55. pageref: address
  56. log:
  57. version: '1.2'
  58. creator:
  59. name: 'PhantomJS'
  60. version: phantom.version.major + '.' + phantom.version.minor + '.' + phantom.version.patch
  61. pages: [
  62. startedDateTime: startTime.toISOString()
  63. id: address
  64. title: title
  65. pageTimings:
  66. onLoad: page.endTime - page.startTime
  67. ]
  68. entries: entries
  69. page = require('webpage').create()
  70. system = require 'system'
  71. if system.args.length is 1
  72. console.log 'Usage: netsniff.coffee <some URL>'
  73. phantom.exit 1
  74. else
  75. page.address = system.args[1]
  76. page.resources = []
  77. page.onLoadStarted = ->
  78. page.startTime = new Date()
  79. page.onResourceRequested = (req) ->
  80. page.resources[req.id] =
  81. request: req
  82. startReply: null
  83. endReply: null
  84. page.onResourceReceived = (res) ->
  85. if res.stage is 'start'
  86. page.resources[res.id].startReply = res
  87. if res.stage is 'end'
  88. page.resources[res.id].endReply = res
  89. page.open page.address, (status) ->
  90. if status isnt 'success'
  91. console.log 'FAIL to load the address'
  92. phantom.exit(1)
  93. else
  94. page.endTime = new Date()
  95. page.title = page.evaluate ->
  96. document.title
  97. har = createHAR page.address, page.title, page.startTime, page.resources
  98. console.log JSON.stringify har, undefined, 4
  99. phantom.exit()