Skip to content

Instantly share code, notes, and snippets.

@krisselden
Last active November 13, 2024 03:12
Show Gist options
  • Save krisselden/277a6882b54a193b01f4556324fa4822 to your computer and use it in GitHub Desktop.
Save krisselden/277a6882b54a193b01f4556324fa4822 to your computer and use it in GitHub Desktop.
#!/usr/bin/env node
import { chromium } from "playwright";
import { readFileSync } from "node:fs";
import { once } from "node:events";
async function main() {
const browser = await chromium.launch({ headless: false });
try {
const cookies = await readCookies("cookies.json");
const context = await browser.newContext({
storageState: {
cookies,
},
});
try {
const page = await context.newPage();
if (process.argv.length > 2) {
await page.goto(process.argv[2]);
}
await page.bringToFront();
await once(page, "close");
} finally {
await context.close();
}
} finally {
await browser.close();
}
}
main();
/**
* @typedef {Object} Cookie;
* @property {string} name
* @property {string} value
* @property {string=} url
* @property {string=} domain
* @property {string=} path
* @property {number=} expires
* @property {boolean=} httpOnly
* @property {boolean=} secure
* @property {"Strict"|"Lax"|"None"=} sameSite
*/
/**
* @param {string} file
* @returns {Cookie[]}
*/
function readCookies(file = "cookies.json") {
try {
return JSON.parse(readFileSync("cookies.json", "utf8"));
} catch (e) {
if (e.code === "ENOENT") {
console.log("No cookies found");
} else {
throw e;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment