diff options
Diffstat (limited to 'castle-search/src/search.ts')
| -rw-r--r-- | castle-search/src/search.ts | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/castle-search/src/search.ts b/castle-search/src/search.ts index 0178349..1ddd523 100644 --- a/castle-search/src/search.ts +++ b/castle-search/src/search.ts @@ -402,16 +402,31 @@ const isGoogleHost = (url: string): boolean => /^https?:\/\/([a-z0-9-]+\.)*googl export const withPage = async <T>( signal: AbortSignal, fn: (page: PageSession, endpoint: string) => Promise<T>, + options: { isolated?: boolean } = {}, ): Promise<T> => { const release = await acquire(signal); try { const conn = await ensureConnection(signal); const endpoint = describeCdpTarget(conn.target); - const page = await PageSession.open(conn, signal); + // isolated: a browser context of its own, with none of the profile's + // cookies, thrown away afterwards with everything in it. + let context: string | undefined; + if (options.isolated) { + const created = await conn.send("Target.createBrowserContext", { disposeOnDetach: true }); + if (typeof created["browserContextId"] !== "string") { + throw new BrowserUnavailableError("Target.createBrowserContext did not return a browserContextId"); + } + context = created["browserContextId"]; + } try { - return await fn(page, endpoint); + const page = await PageSession.open(conn, signal, context); + try { + return await fn(page, endpoint); + } finally { + await page.close(); + } } finally { - await page.close(); + if (context) await conn.send("Target.disposeBrowserContext", { browserContextId: context }).catch(() => {}); } } finally { release(); |
