Skip to content

Instantly share code, notes, and snippets.

@dongyuwei
Created September 10, 2012 09:33
Show Gist options
  • Save dongyuwei/3689928 to your computer and use it in GitHub Desktop.
Save dongyuwei/3689928 to your computer and use it in GitHub Desktop.
phantomjs auto netsniff, create HAR file.
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = function () {
function pad(n) { return n < 10 ? '0' + n : n; }
function ms(n) { return n < 10 ? '00'+ n : n < 100 ? '0' + n : n }
return this.getFullYear() + '-' +
pad(this.getMonth() + 1) + '-' +
pad(this.getDate()) + 'T' +
pad(this.getHours()) + ':' +
pad(this.getMinutes()) + ':' +
pad(this.getSeconds()) + '.' +
ms(this.getMilliseconds()) + 'Z';
}
}
function createHAR(address, title, startTime, resources)
{
var entries = [];
resources.forEach(function (resource) {
var request = resource.request,
startReply = resource.startReply,
endReply = resource.endReply;
if (!request || !startReply || !endReply) {
return;
}
entries.push({
startedDateTime: request.time.toISOString(),
time: endReply.time - request.time,
request: {
method: request.method,
url: request.url,
httpVersion: "HTTP/1.1",
cookies: [],
headers: request.headers,
queryString: [],
headersSize: -1,
bodySize: -1
},
response: {
status: endReply.status,
statusText: endReply.statusText,
httpVersion: "HTTP/1.1",
cookies: [],
headers: endReply.headers,
redirectURL: "",
headersSize: -1,
bodySize: startReply.bodySize,
content: {
size: startReply.bodySize,
mimeType: endReply.contentType
}
},
cache: {},
timings: {
blocked: 0,
dns: -1,
connect: -1,
send: 0,
wait: startReply.time - request.time,
receive: endReply.time - startReply.time,
ssl: -1
},
pageref: address
});
});
return {
log: {
version: '1.2',
creator: {
name: "PhantomJS",
version: phantom.version.major + '.' + phantom.version.minor +
'.' + phantom.version.patch
},
pages: [{
startedDateTime: startTime.toISOString(),
id: address,
title: title,
pageTimings: {
onLoad: page.endTime - page.startTime
}
}],
entries: entries
}
};
}
var page = require('webpage').create();
page.address = 'http://www.lightinthebox.com/';
page.resources = [];
page.onLoadStarted = function () {
page.startTime = new Date();
};
page.onResourceRequested = function (req) {
page.resources[req.id] = {
request: req,
startReply: null,
endReply: null
};
};
page.onResourceReceived = function (res) {
if (res.stage === 'start') {
page.resources[res.id].startReply = res;
}
if (res.stage === 'end') {
page.resources[res.id].endReply = res;
}
};
page.open(page.address, function (status) {
var har;
if (status !== 'success') {
console.log('FAIL to load the address');
} else {
page.endTime = new Date();
page.title = page.evaluate(function () {
return document.title;
});
har = createHAR(page.address, page.title, page.startTime, page.resources);
console.log(JSON.stringify(har, undefined, 4));
}
phantom.exit();
});
@flavio-araujo
Copy link

How can I generat har file with and without cache? Example: http://www.janodvarko.cz/har/viewer/?path=examples/softwareishard.com.har

(If the link above not work, see http://www.janodvarko.cz/har/viewer/ and then click on third option Browser cache)

@SADHASIVAM-BE
Copy link

I would like to get request.cookies[] and response.cookies[] information. Is it possible ? I tried to get from phontom.cookie, But not handling domain restriction and expires. Please advice me to implement this functionality. Also, Is there any callback available for each cookie updates (phantom.cookie). Thanks in Advance.

@koalamon
Copy link

Is it possible that requests that respond with 204 do not appear in the har file?

@marcus-wu
Copy link

The if (!request || !startReply || !endReply) { causes pending requests to not show up.

@caisunjun
Copy link

How to run https pages use phantomjs netsniff.js ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment