Skip to content

Instantly share code, notes, and snippets.

@rafcontreras
Last active May 19, 2020 02:43
Show Gist options
  • Save rafcontreras/5bdcd3053772145401bf99eabc3e10c3 to your computer and use it in GitHub Desktop.
Save rafcontreras/5bdcd3053772145401bf99eabc3e10c3 to your computer and use it in GitHub Desktop.
Create a cookie array from cookies.txt file
const parseTokens = (tokens) => {
if (tokens.length === 7) {
const domain = tokens[0];
const flag = tokens[1] === "TRUE";
const path = tokens[2];
const secure = tokens[3] === "TRUE";
let expiration = tokens[4];
if (expiration.length === 17) {
expiration = Math.floor(expiration / 1000000 - 11644473600);
}
const name = tokens[5];
const value = tokens[6];
return {
domain,
flag,
path,
secure,
expiration,
name,
value,
};
}
return null;
};
const extractCookies = (text) => {
const cookies = text
.split("\n")
.map((line) => {
const tokens = line.split("\t").map((token) => token.trim());
return parseTokens(tokens);
})
.filter((cookie) => cookie !== null);
return cookies;
};
export default extractCookies;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment