Skip to content

Commit 042282c

Browse files
authored
chore: update docs (#4448)
1 parent c6d6acb commit 042282c

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

headless-chromium/README.md

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,51 @@ This template starts a headless chromium browser, which you can connect to with
55
For example, here's a snippet to show how you can connect to this using Playwright:
66

77
```js
8-
const { chromium } = require('playwright');
8+
const { chromium } = require("playwright");
99

10+
// Replace with your sandbox id
11+
const sandboxId = "zwyx95";
1012
(async () => {
13+
// Get the debugger URL
14+
const debuggerUrl = await fetch(
15+
`https://${sandboxId}-9222.csb.app/json/version`,
16+
{
17+
headers: {
18+
"x-csb-host": "localhost:9222",
19+
},
20+
}
21+
)
22+
.then((res) => res.json())
23+
.then((data) => {
24+
// Chrome bases the debugger URL on the host header, so we have to change it back to the public
25+
// interface.
26+
return data.webSocketDebuggerUrl
27+
.replace("ws://", "wss://")
28+
.replace("localhost", `${sandboxId}-9222.csb.app`)
29+
.replace(":9222", "");
30+
});
31+
1132
// Connect to the Chromium instance over CDP.
1233
// This URL corresponds to the remote debugging endpoint exposed by your Docker container.
13-
const browser = await chromium.connectOverCDP('https://$SANDBOX_ID-9222.csb.app');
34+
const browser = await chromium.connectOverCDP(debuggerUrl, {
35+
headers: {
36+
"x-csb-host": "localhost:9222",
37+
},
38+
});
1439

1540
// Retrieve an existing browser context or create a new one.
1641
const contexts = browser.contexts();
1742
const context = contexts.length ? contexts[0] : await browser.newContext();
1843

1944
// Open a new page and navigate to a URL.
2045
const page = await context.newPage();
21-
await page.goto('https://example.com');
46+
await page.goto("https://example.com");
2247

2348
// Log the page title.
2449
console.log(await page.title());
2550

2651
// Close the browser when done.
2752
await browser.close();
2853
})();
54+
2955
```

0 commit comments

Comments
 (0)