Skip to content

Instantly share code, notes, and snippets.

@titulus
Last active January 20, 2024 02:00
Show Gist options
  • Save titulus/2dbd0893c036328d2e8a3d953207eb61 to your computer and use it in GitHub Desktop.
Save titulus/2dbd0893c036328d2e8a3d953207eb61 to your computer and use it in GitHub Desktop.
parse cookies.txt into array of object used by puppeteer
const fs = require('fs');
const file = fs.readFileSync('./cookies.txt', 'utf8');
const cookies = file
.split('\n')
.map(line => line2object(''+line))
.filter(notNull => notNull);
module.exports = cookies;
function line2object (line) {
const matches = line.match(/^([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)$/);
if (!matches)
return null;
const [
all,
domain,
tailmatch,
path,
secure,
expires,
name,
value,
] = Array.from(matches);
return {
name,
value,
domain,
path,
expires: +expires,
secure: secure === 'TRUE',
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment