Path: blob/master/6-Selenium/phantomjs/examples/netsniff.js
164 views
if (!Date.prototype.toISOString) {1Date.prototype.toISOString = function () {2function pad(n) { return n < 10 ? '0' + n : n; }3function ms(n) { return n < 10 ? '00'+ n : n < 100 ? '0' + n : n }4return this.getFullYear() + '-' +5pad(this.getMonth() + 1) + '-' +6pad(this.getDate()) + 'T' +7pad(this.getHours()) + ':' +8pad(this.getMinutes()) + ':' +9pad(this.getSeconds()) + '.' +10ms(this.getMilliseconds()) + 'Z';11}12}1314function createHAR(address, title, startTime, resources)15{16var entries = [];1718resources.forEach(function (resource) {19var request = resource.request,20startReply = resource.startReply,21endReply = resource.endReply;2223if (!request || !startReply || !endReply) {24return;25}2627// Exclude Data URI from HAR file because28// they aren't included in specification29if (request.url.match(/(^data:image\/.*)/i)) {30return;31}3233entries.push({34startedDateTime: request.time.toISOString(),35time: endReply.time - request.time,36request: {37method: request.method,38url: request.url,39httpVersion: "HTTP/1.1",40cookies: [],41headers: request.headers,42queryString: [],43headersSize: -1,44bodySize: -145},46response: {47status: endReply.status,48statusText: endReply.statusText,49httpVersion: "HTTP/1.1",50cookies: [],51headers: endReply.headers,52redirectURL: "",53headersSize: -1,54bodySize: startReply.bodySize,55content: {56size: startReply.bodySize,57mimeType: endReply.contentType58}59},60cache: {},61timings: {62blocked: 0,63dns: -1,64connect: -1,65send: 0,66wait: startReply.time - request.time,67receive: endReply.time - startReply.time,68ssl: -169},70pageref: address71});72});7374return {75log: {76version: '1.2',77creator: {78name: "PhantomJS",79version: phantom.version.major + '.' + phantom.version.minor +80'.' + phantom.version.patch81},82pages: [{83startedDateTime: startTime.toISOString(),84id: address,85title: title,86pageTimings: {87onLoad: page.endTime - page.startTime88}89}],90entries: entries91}92};93}9495var page = require('webpage').create(),96system = require('system');9798if (system.args.length === 1) {99console.log('Usage: netsniff.js <some URL>');100phantom.exit(1);101} else {102103page.address = system.args[1];104page.resources = [];105106page.onLoadStarted = function () {107page.startTime = new Date();108};109110page.onResourceRequested = function (req) {111page.resources[req.id] = {112request: req,113startReply: null,114endReply: null115};116};117118page.onResourceReceived = function (res) {119if (res.stage === 'start') {120page.resources[res.id].startReply = res;121}122if (res.stage === 'end') {123page.resources[res.id].endReply = res;124}125};126127page.open(page.address, function (status) {128var har;129if (status !== 'success') {130console.log('FAIL to load the address');131phantom.exit(1);132} else {133page.endTime = new Date();134page.title = page.evaluate(function () {135return document.title;136});137har = createHAR(page.address, page.title, page.startTime, page.resources);138console.log(JSON.stringify(har, undefined, 4));139phantom.exit();140}141});142}143144145