diff options
| author | Igor Soarez <igor@soarez.org> | 2026-11-18 10:50:14 +0000 |
|---|---|---|
| committer | Igor Soarez <igor@soarez.org> | 2026-11-18 10:50:14 +0000 |
| commit | 910ac7cd424f6401ea042a84603c42ba055994f8 (patch) | |
| tree | 3ff40da7d14c644428c71cab1b005eca0b186d13 /castle-search/src/cdp.ts | |
| parent | 39f887e07721c5b3ddd31657e5e8e25e4486d2e5 (diff) | |
castle-search: sign X back in when a search finds it signed out
Username, password, the authenticator code (TOTP) and, when X asks, the
code it emails, read over IMAP. At most one try per 30 minutes, the
password never sent twice, and any unknown step (a captcha) fails saying
a person has to sign in. Details from the file CCS_X_LOGIN_FILE names.
Diffstat (limited to 'castle-search/src/cdp.ts')
| -rw-r--r-- | castle-search/src/cdp.ts | 28 |
1 files changed, 26 insertions, 2 deletions
diff --git a/castle-search/src/cdp.ts b/castle-search/src/cdp.ts index aa01b56..64c382e 100644 --- a/castle-search/src/cdp.ts +++ b/castle-search/src/cdp.ts @@ -242,7 +242,12 @@ export class PageSession { this.#sessionId = sessionId; } - static async open(conn: BrowserConnection, signal?: AbortSignal): Promise<PageSession> { + /** + * `browserContextId`, from Target.createBrowserContext, opens the page in a + * context of its own, with no cookies: used to try a sign-in without + * touching the browser's own signed-in session. + */ + static async open(conn: BrowserConnection, signal?: AbortSignal, browserContextId?: string): Promise<PageSession> { if (signal?.aborted) throw abortError(signal); // Deliberately NOT raced against the abort signal. raceAbort abandons the @@ -251,7 +256,11 @@ export class PageSession { // using, with nothing left holding its targetId to close it. It is a fast // command already bounded by COMMAND_TIMEOUT_MS, so waiting it out costs // little and guarantees we own whatever it created. - const created = await conn.send("Target.createTarget", { url: "about:blank", background: true }); + const created = await conn.send("Target.createTarget", { + url: "about:blank", + background: true, + ...(browserContextId ? { browserContextId } : {}), + }); const targetId = created["targetId"]; if (typeof targetId !== "string") { throw new BrowserUnavailableError("Target.createTarget did not return a targetId"); @@ -331,6 +340,21 @@ export class PageSession { } /** + * Types `text` into whatever has focus, as a paste would: one input event, so + * a framework-controlled field (React's) sees it. Never logged. + */ + async insertText(text: string, signal?: AbortSignal): Promise<void> { + await raceAbort(this.#conn.send("Input.insertText", { text }, this.#sessionId), signal); + } + + /** Presses and releases Enter in whatever has focus. */ + async pressEnter(signal?: AbortSignal): Promise<void> { + const key = { key: "Enter", code: "Enter", windowsVirtualKeyCode: 13 }; + await raceAbort(this.#conn.send("Input.dispatchKeyEvent", { type: "keyDown", ...key, text: "\r" }, this.#sessionId), signal); + await raceAbort(this.#conn.send("Input.dispatchKeyEvent", { type: "keyUp", ...key }, this.#sessionId), signal); + } + + /** * Close the tab. Idempotent and never throws: it runs on error paths, where a * second failure would mask the one worth reporting. */ |
