fix: define browser context

This commit is contained in:
amhsirak
2025-11-17 19:49:38 +05:30
parent 01a2678358
commit 994142ae40

View File

@@ -1,4 +1,4 @@
import { chromium, Browser, Page } from 'playwright'; import { chromium, Browser, Page, BrowserContext } from 'playwright';
export interface GetPageSourceOptions { export interface GetPageSourceOptions {
wait?: number; wait?: number;
@@ -17,6 +17,7 @@ export async function getPageSource(
} = options; } = options;
let browser: Browser | null = null; let browser: Browser | null = null;
let context: BrowserContext | null = null;
let page: Page | null = null; let page: Page | null = null;
try { try {
@@ -25,8 +26,8 @@ export async function getPageSource(
args: ['--no-sandbox', '--disable-dev-shm-usage'] args: ['--no-sandbox', '--disable-dev-shm-usage']
}); });
page = await browser.newPage(); context = await browser.newContext({ userAgent });
await page.setUserAgent(userAgent); page = await context.newPage();
// Convert wait time to milliseconds // Convert wait time to milliseconds
const waitMs = wait * 1000; const waitMs = wait * 1000;
@@ -45,9 +46,9 @@ export async function getPageSource(
} catch (error) { } catch (error) {
console.error('Error while getting page source: ', error); console.error('Error while getting page source: ', error);
return '';
} finally { } finally {
if (page) await page.close(); if (page) await page.close();
if (context) await context.close();
if (browser) await browser.close(); if (browser) await browser.close();
} }
} }