File size: 1,831 Bytes
5a0b87c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { chromium, Browser, BrowserContext, Page } from "playwright";

class BrowserManager {
    private browser: Browser | null = null;
    private context: BrowserContext | null = null;
    private page: Page | null = null;

    async init() {
        if (!this.browser) {
            this.browser = await chromium.launch({ headless: true });
            this.context = await this.browser.newContext({
                viewport: { width: 1280, height: 800 },
                userAgent:
                    "Mozilla/5.0 (Windows NT 10.0; Win64; x64) CodeVerseAgent/1.0",
            });
            this.page = await this.context.newPage();
        }
    }

    async navigate(url: string) {
        await this.init();
        await this.page!.goto(url, { waitUntil: "domcontentloaded" });
        return this.page!.url();
    }

    async click(selector: string) {
        await this.page!.click(selector);
    }

    async type(selector: string, text: string) {
        await this.page!.fill(selector, text);
    }

    async getSnapshot() {
        await this.init();

        // Use aria snapshot (Playwright v1.47+) instead of deprecated accessibility API
        const ariaSnapshot = await this.page!.locator("body").ariaSnapshot();
        const screenshot = await this.page!.screenshot({
            type: "jpeg",
            quality: 60,
        });

        return {
            url: this.page!.url(),
            title: await this.page!.title(),
            domSnapshot: ariaSnapshot,
            screenshotBase64: screenshot.toString("base64"),
        };
    }

    async close() {
        if (this.browser) {
            await this.browser.close();
            this.browser = null;
            this.context = null;
            this.page = null;
        }
    }
}

export const browserManager = new BrowserManager();