Skip to content

Instantly share code, notes, and snippets.

@simioni
Forked from amishshah/har-extract.js
Last active August 25, 2021 13:17
Show Gist options
  • Save simioni/951dd44f16ef2eafe852567b1a0e1557 to your computer and use it in GitHub Desktop.
Save simioni/951dd44f16ef2eafe852567b1a0e1557 to your computer and use it in GitHub Desktop.
Rough script to extract images from HTTP Archive (HAR) files
const fs = require('fs');
const fsAsync = require('fs').promises;
const targetMimeType = 'image/jpeg';
const file = JSON.parse(fs.readFileSync('./dump.har')).log;
const dir = './output';
if (!fs.existsSync(dir)){
fs.mkdirSync(dir);
}
// renders a text based progress bar to the console
const width = 30;
const displayProgress = (cur, total) => {
const pct = Math.round(cur / total * 100) / 100;
const done = pct * width;
const remaining = width - done;
const filled = '\u2588'.repeat(done);
const empty = '\u2591'.repeat(remaining);
// '\r' clears the current line in stout
process.stdout.write(`\r${filled}${empty} | ${Math.ceil(pct * 100)}% | ${cur} of ${total} images saved.`);
}
const promises = [];
let started = 0;
let finished = 0;
for (const entry of file.entries) {
if (entry.response.content.mimeType === targetMimeType) {
const pathParts = new URL(entry.request.url).pathname.split('/');
const filename = pathParts.pop() || pathParts.pop(); // Pop twice to avoid potential trailing slash
promises.push(fsAsync.writeFile(`${dir}/${filename}`, entry.response.content.text, 'base64')
.then(() => {
finished++;
displayProgress(finished, started);
})
.catch(err => {
console.log(err)
})
);
started++;
}
}
Promise.all(promises).then(() => {
process.stdout.write(`\n\u2713 Done.`);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment