Files
2026-03-03 23:49:13 +01:00

36 lines
1.3 KiB
JavaScript

/**
* Wraps Playwright's CDPSession (from context.getExistingCDPSession) into an ICDPSession.
* This reuses Playwright's internal CDP WebSocket instead of creating a new one,
* which is important for the relay server where Target.attachToTarget is intercepted.
*/
export class PlaywrightCDPSessionAdapter {
_playwrightSession;
constructor(playwrightSession) {
this._playwrightSession = playwrightSession;
}
async send(method, params) {
return await this._playwrightSession.send(method, params);
}
on(event, callback) {
this._playwrightSession.on(event, callback);
return this;
}
off(event, callback) {
this._playwrightSession.off(event, callback);
return this;
}
async detach() {
await this._playwrightSession.detach();
}
}
/**
* Gets a CDP session for a page by reusing Playwright's internal existing CDP session.
* This uses the same WebSocket Playwright already has, avoiding new connections.
* Works through the relay because it doesn't call Target.attachToTarget.
*/
export async function getCDPSessionForPage({ page }) {
const context = page.context();
const playwrightSession = await context.getExistingCDPSession(page);
return new PlaywrightCDPSessionAdapter(playwrightSession);
}
//# sourceMappingURL=cdp-session.js.map