|
| 1 | +# browser-session |
| 2 | + |
| 3 | +[](https://github.com/jpoindexter/browser-session/actions/workflows/ci.yml) |
| 4 | +[](LICENSE) |
| 5 | +[](https://nodejs.org) |
| 6 | +[](https://www.typescriptlang.org/) |
| 7 | + |
| 8 | +**Reuse your real logged-in browser session from Node.** Two things: |
| 9 | + |
| 10 | +1. **Read + decrypt your browser cookies** (Brave / Chrome / Edge) — **zero dependencies**. The Node equivalent of yt-dlp's `--cookies-from-browser`. |
| 11 | +2. **Open any page in a headless browser with that session** — read login-walled / JS-rendered pages a plain `fetch` can't (via Playwright). |
| 12 | + |
| 13 | +No extension, no manual cookie export. |
| 14 | + |
| 15 | +> ⚠️ **macOS only for now** (the cookie key lives in the login Keychain — one approval prompt the first time). The headless-read part is cross-platform; only the cookie auto-read is macOS-specific so far. Use a dedicated account for scraping — automated access can get accounts flagged. |
| 16 | +
|
| 17 | +## Install |
| 18 | + |
| 19 | +```bash |
| 20 | +npm install browser-session |
| 21 | +# for the headless-read part: |
| 22 | +npm install playwright-core && npx playwright install chromium |
| 23 | +``` |
| 24 | + |
| 25 | +## Use |
| 26 | + |
| 27 | +### Library |
| 28 | + |
| 29 | +```ts |
| 30 | +import { getCookies, openWithSession, readPage } from "browser-session"; |
| 31 | + |
| 32 | +// 1) just the cookies (zero-dep) |
| 33 | +const c = getCookies({ browser: "brave", domain: "x.com" }); |
| 34 | +if (c.ok) console.log(c.cookie); // "auth_token=…; ct0=…; …" |
| 35 | + |
| 36 | +// 2) read a logged-in page in a real browser, session auto-injected |
| 37 | +const r = await readPage("https://www.reddit.com/", { browser: "brave" }); |
| 38 | +if (r.ok) console.log(r.text); // rendered text of YOUR feed |
| 39 | +// console.log(r.requests); // every URL the page requested |
| 40 | + |
| 41 | +// 3) bring your own cookie (any source) |
| 42 | +await openWithSession("https://x.com/i/bookmarks", "auth_token=…; ct0=…"); |
| 43 | +``` |
| 44 | + |
| 45 | +Every call returns `{ ok: true, … } | { ok: false, error }` — errors as values, never throws. |
| 46 | + |
| 47 | +### CLI |
| 48 | + |
| 49 | +```bash |
| 50 | +browser-session cookies x.com --browser brave # print the cookie header |
| 51 | +browser-session cookies x.com --names # just the cookie names |
| 52 | +browser-session read https://www.reddit.com/ --browser brave # render your logged-in page |
| 53 | +browser-session read https://example.com # anonymous read |
| 54 | +``` |
| 55 | + |
| 56 | +## How it works |
| 57 | + |
| 58 | +- **Cookies** (`src/cookies.ts`): pulls the AES key from the macOS Keychain (`<Browser> Safe Storage`), derives it (`PBKDF2(pw, "saltysalt", 1003, 16, sha1)`), reads the Cookies SQLite (copied to dodge the browser lock via the system `sqlite3`), and AES-128-CBC-decrypts each value. Handles the `v10`/`v11` prefix **and** the 32-byte `SHA256(host)` prefix newer Chromium prepends (stripped when the decrypted head isn't printable). Zero runtime deps. |
| 59 | +- **Session** (`src/session.ts`): launches headless Chromium (Playwright), injects the cookies scoped to the page origin (so `__Host-`/`__Secure-` prefixes resolve), navigates, and returns the body text + the request URLs. `playwright-core` is an **optional** dependency — the cookie reader works without it. |
| 60 | + |
| 61 | +## API |
| 62 | + |
| 63 | +`getCookies({ browser?, domain, profile? })` · `decryptCookie(hex, key)` · `openWithSession(url, cookie, { settleMs? })` · `readPage(url, { browser?, profile? })` · `cookieToPlaywright(header, url)` · `domainOf(url)`. |
| 64 | + |
| 65 | +## License |
| 66 | + |
| 67 | +MIT |
0 commit comments