-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
65 lines (55 loc) · 1.77 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import puppeteer from "puppeteer-core";
let sessionId: string;
async function createSession() {
const response = await fetch(`https://www.browserbase.com/v1/sessions`, {
method: "POST",
headers: {
'x-bb-api-key': `${process.env.BROWSERBASE_API_KEY}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
projectId: process.env.BROWSERBASE_PROJECT_ID,
}),
});
const json = await response.json();
return json;
}
async function retrieveDebugConnectionURL(sessionId: string) {
const response = await fetch(
`https://www.browserbase.com/v1/sessions/${sessionId}/debug`,
{
method: "GET",
headers: {
'x-bb-api-key': `${process.env.BROWSERBASE_API_KEY}`,
},
},
);
const json = await response.json();
return json.debuggerFullscreenUrl;
}
(async () => {
const { id } = await createSession();
sessionId = id;
console.log("Starting remote browser...")
const browser = await puppeteer.connect({
browserWSEndpoint:
`wss://connect.browserbase.com?apiKey=${process.env.BROWSERBASE_API_KEY}`
});
const page = await browser.newPage();
await page.goto("https://www.browserbase.com", {
// let's make sure the page is fully loaded before asking for the live debug URL
waitUntil: "domcontentloaded",
});
const debugUrl = await retrieveDebugConnectionURL(sessionId);
console.log(`Session started, live debug accessible here: ${debugUrl}.`);
console.log("Taking a screenshot!")
await page.screenshot({ fullPage: true })
console.log("Shutting down...")
await page.close();
await browser.close();
})().catch((error) => {
console.log(
`Session failed, replay is accessible here: https://www.browserbase.com/sessions/${sessionId}.`,
);
console.error(error.message);
});