Example/ability to parse Netscape HTTP Cookie File
brandonros opened this issue · comments
Brandon Ros commented
https://curl.se/docs/http-cookies.html
import fetch from 'node-fetch'
import tough from 'tough-cookie'
import cookiefile from 'cookiefile' // a 6 year old unupdated package that doesn't support true ESM imports
const run = async () => {
const cookieJar = new tough.CookieJar()
const cookieMap = new cookiefile.CookieMap('./all_cookies.txt')
const keys = cookieMap.keys()
for (const key of keys) {
const cookieMapCookie = cookieMap.get(key)
const cookie = new tough.Cookie({
key: cookieMapCookie.cookieName,
value: cookieMapCookie.value,
expires: cookieMapCookie.expire !== 0 ? new Date(cookieMapCookie.expire * 1000) : 'Infinity',
// TODO: maxAge?
domain: cookieMapCookie.domain,
path: cookieMapCookie.path,
secure: cookieMapCookie.https,
httpOnly: cookieMapCookie.httpOnly,
//sameSite: cookieMapCookie.crossDomain === true ? 'none' : 'lax' // TODO: not sure about this?
})
const url = `${cookieMapCookie.https ? 'https' : 'http'}://${cookieMapCookie.domain}${cookieMapCookie.path}`
console.log({
cookie,
url
})
cookieJar.setCookieSync(cookie, url)
}
const cookies = cookieJar.getCookieStringSync('https://redacted')
console.log(cookies)
}
run()
This doesn't really work super well/I'm wondering if I'm missing something. I thought this would've been a common use case. I don't see a way to export Google Chrome cookies to any other format (even with an extension).
I don't see a way to export them to JSON. I figured Chrome cookies -> cookies.txt or cookies.json -> parse in tough-cookie would be semi-common. Not sure if it is worth putting resources into an example/test case for this?
Brandon Ros commented
https://github.com/JSBizon/file-cookie-store found this. pretty old but I'll give it a shot.