Repository navigation
Expand file tree
/
Copy pathextension-basic.ts
More file actions
53 lines (43 loc) · 1.53 KB
/
Copy pathextension-basic.ts
File metadata and controls
53 lines (43 loc) · 1.53 KB
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
import { access } from 'node:fs/promises';
import { ensureCredentials } from './quickstart-auth.ts';
import { chromium, type Browser } from 'playwright';
import { Lexmount } from 'lexmount';
async function main(): Promise<void> {
await ensureCredentials();
const extensionPath = process.env.LEXMOUNT_EXTENSION_PATH?.trim();
if (!extensionPath) {
throw new Error('LEXMOUNT_EXTENSION_PATH is required for extension-basic.ts');
}
await access(extensionPath);
const client = new Lexmount();
let browser: Browser | undefined;
const extension = await client.extensions.upload(extensionPath, {
name: 'quickstart-extension',
});
console.log(`Uploaded extension: ${extension.id}`);
const extensions = await client.extensions.list({ limit: 10 });
console.log(`Found ${extensions.length} extension(s)`);
const session = await client.sessions.create({
browserMode: 'normal',
extensionIds: [extension.id],
});
console.log(`Session created: ${session.id}`);
try {
browser = await chromium.connectOverCDP(session.connectUrl);
const context = browser.contexts()[0];
const page = context?.pages()[0] ?? (await context?.newPage());
if (!page) {
throw new Error('No page available after connecting to the remote browser.');
}
await page.goto('https://www.baidu.com/');
console.log(`Page title: ${await page.title()}`);
} finally {
await browser?.close();
await session.close();
client.close();
}
}
main().catch((error: unknown) => {
console.error(error);
process.exit(1);
});