feat:dynamic viewport for browser context

This commit is contained in:
karishmas6
2024-10-22 02:43:28 +05:30
parent 8c7101daf1
commit f35b8a7d60

View File

@@ -30,6 +30,8 @@ export class RemoteBrowser {
*/
private browser: Browser | null = null;
private context: BrowserContext | null = null;
/**
* The Playwright's [CDPSession](https://playwright.dev/docs/api/class-cdpsession) instance,
* used to talk raw Chrome Devtools Protocol.
@@ -90,13 +92,13 @@ export class RemoteBrowser {
*/
public initialize = async (options: RemoteBrowserOptions): Promise<void> => {
this.browser = <Browser>(await options.browser.launch(options.launchOptions));
const context = await this.browser.newContext(
{
viewport: { height: 400, width: 900 },
// recordVideo: { dir: 'videos/' }
}
this.context = await this.browser.newContext(
// {
// viewport: { height: 400, width: 900 },
// // recordVideo: { dir: 'videos/' }
// }
);
this.currentPage = await context.newPage();
this.currentPage = await this.context.newPage();
const blocker = await PlaywrightBlocker.fromPrebuiltAdsAndTracking(fetch);
await blocker.enableBlockingInPage(this.currentPage);
this.client = await this.currentPage.context().newCDPSession(this.currentPage);
@@ -138,6 +140,16 @@ export class RemoteBrowser {
logger.log('error', `${tabInfo.index} index out of range of pages`)
}
});
this.socket.on('setViewportSize', async (data: { width: number, height: number }) => {
const { width, height } = data;
logger.log('debug', `Received viewport size: width=${width}, height=${height}`);
// Update the browser context's viewport dynamically
if (this.context && this.browser) {
this.context = await this.browser.newContext({ viewport: { width, height } });
logger.log('debug', `Viewport size updated to width=${width}, height=${height} for the entire browser context`);
}
});
}
/**