lino-levan / astral

A high-level puppeteer/playwright-like library for Deno
https://jsr.io/@astral/astral
MIT License
176 stars 7 forks source link

request: provide an example of connecting to an existing browser #59

Closed hunterloftis closed 3 weeks ago

hunterloftis commented 2 months ago

The documentation only shows how to connect to a cloud-hosted browser service: https://astral.deno.dev/advanced/connect/

Using Astral, how would you connect to a locally-running browser?

lino-levan commented 2 months ago

Seems like a reasonable thing to document. Essentially the process is as simple as running a chromium-like binary with the following flags:

--remote-debugging-port=<SOME_PORT>
--headless=new
--no-first-run
--password-store=basic
--use-mock-keychain
--hide-scrollbars

Technically, only the first one is necessary, though I've found that these flags generally get the best result. One this is running, it'd be as simple as

// Connect to remote endpoint
const browser = await launch({
  wsEndpoint: "wss://localhost:<SOME_PORT>",
});

// Do stuff
const page = await browser.newPage("http://example.com");
console.log(await page.evaluate(() => document.title));

// Close connection
await browser.close();

to connect to it. This should definitely be documented though, thanks for opening the issue.