Created
January 31, 2025 18:10
-
-
Save charneykaye/883e6dd136c1df11db8e3434eaed9c3b to your computer and use it in GitHub Desktop.
Parse a Chrome HAR export into a CSV of all the network resource transactions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Parse the output of a Chrome .HAR and output a simple CSV with the URL and size in bytes (one per row) of every file loaded | |
// Usage: node parse.js <input.har> <output.csv> | |
// Example: node parse.js input.har output.csv | |
var fs = require('fs'); | |
if (process.argv.length < 4) { | |
console.log('Usage: node parse.js <input.har> <output.csv>'); | |
process.exit(1); | |
} | |
var input = process.argv[2]; | |
var output = process.argv[3]; | |
var data = fs.readFileSync(input, 'utf8'); | |
var parsed = JSON.parse(data); | |
var entries = parsed.log.entries; | |
var csv = 'RequestMethod,RequestURL,RequestBytes,ResponseStatus,ResponseBytes\n'; | |
for (var i = 0; i < entries.length; i++) { | |
var entry = entries[i]; | |
var iMethod = entry.request.method; | |
var iUrl = entry.request.url; | |
var iBytes = entry.request.bodySize; | |
var oStatus = entry.response.status; | |
var oBytes = entry.response.content.size; | |
csv += _safe(iMethod) + ',' + _safe(iUrl) + ',' + _safe(iBytes) + ',' + _safe(oStatus) + ',' + _safe(oBytes) + '\n'; | |
} | |
fs.writeFileSync(output, csv); | |
console.log('Done!'); | |
// Helper function to escape CSV values | |
function _safe(str) { | |
if (str === undefined) { | |
return ''; | |
} | |
if (typeof str === 'number') { | |
return str; | |
} | |
if (typeof str !== 'string') { | |
return '"<object>"'; | |
} | |
return '"' + str.replace(/"/g, '""') + '"'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment