aboutsummaryrefslogtreecommitdiff
path: root/castle-search/src/cdp.ts
diff options
context:
space:
mode:
Diffstat (limited to 'castle-search/src/cdp.ts')
-rw-r--r--castle-search/src/cdp.ts28
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.
*/