Files
RSSHub/lib/utils/playwright.worker.ts
T
Tony cfcdc34e1f refactor: replace playwright with patchright (#21976)
* refactor: use patchwright

* refactor: migrate setRequestInterception and waitUntil

* fix: overwrite ua

* refactor: drop puppeteer cookie/requestfinished shims

Migrate page.cookies/setCookie and browser.cookies/setCookie call sites to the native context APIs (page.context().cookies/addCookies), rewrite requestfinished handlers to await request.response() instead of consuming the synchronous puppeteer-shaped payload, and remove the now-redundant browser.close context-cleanup wrapper. Playwright already handles all of these natively; the shims existed only for the puppeteer -> playwright migration.

* refactor: remove page.authenticate, page.setUserAgent,  browser.newPage(), browser.userAgent() shims

update cookie utility tests

* refactor: replace browser instance with context in Playwright usage across multiple routes

- Updated all instances of `browser` to `context` in Playwright-related functions for consistency and improved resource management.
- Ensured proper closure of context after operations to prevent memory leaks.
- Adjusted request interception methods to utilize the new context variable.

* fix: direct type import

* refactor: remove Puppeteer compatibility layers and unify page handling with Playwright

* fix: test case

* chore: bump patchright to v1.60.0

* chore: pin patchright to 1.59.1

nft can't resolve browser.json due to https://github.com/microsoft/playwright/pull/40093

* chore: pin to node 24.15 for playwright

browser install on node 24.16 stucks due to https://github.com/microsoft/playwright/issues/40724

* fix: update patchright and patchright-core to version 1.60.2 and adjust minify-docker.js for compatibility

* fix: import BrowserContext from patchright
2026-06-07 02:43:20 +08:00

87 lines
2.3 KiB
TypeScript

// Worker-compatible Playwright using Cloudflare Browser Run.
import type { Browser, Page } from '@cloudflare/playwright';
import { launch } from '@cloudflare/playwright';
import logger from './logger';
type GotoOptions = Parameters<Page['goto']>[1];
let browserBinding: any = null;
export const setBrowserBinding = (binding: any) => {
browserBinding = binding;
};
const getBrowserBinding = () => {
if (!browserBinding) {
throw new Error('Browser Run API not available. This route requires Cloudflare Browser Run which is only available in remote mode. Use `wrangler dev --remote` or deploy to Cloudflare Workers.');
}
return browserBinding;
};
const launchBrowser = async () => {
const browser = await launch(getBrowserBinding(), { keep_alive: 60000 });
const context = await browser.newContext({
ignoreHTTPSErrors: true,
});
return { browser, context };
};
const scheduleClose = (browser: Browser) => {
setTimeout(() => {
void browser.close();
}, 30000);
};
/**
* @returns Playwright browser context (native `newPage()` shares state across calls)
*/
const outPlaywright = async () => {
const { browser, context } = await launchBrowser();
scheduleClose(browser);
return context;
};
export default outPlaywright;
/**
* @returns Playwright page
*/
export const getPlaywrightPage = async (
url: string,
instanceOptions: {
gotoConfig?: GotoOptions;
noGoto?: boolean;
onBeforeLoad?: (page: Page, context?: Awaited<ReturnType<typeof launchBrowser>>['context']) => Promise<void> | void;
} = {}
) => {
logger.debug(`Launching Cloudflare Browser for: ${url}`);
const { browser, context } = await launchBrowser();
scheduleClose(browser);
const page = await context.newPage();
if (instanceOptions.onBeforeLoad) {
await instanceOptions.onBeforeLoad(page, context);
}
if (!instanceOptions.noGoto) {
try {
await page.goto(url, instanceOptions.gotoConfig || { waitUntil: 'domcontentloaded' });
} catch (error) {
logger.error(`Playwright navigation failed: ${error}`);
throw error;
}
}
return {
context,
destroy: async () => {
await context.close();
},
page,
};
};
export { type Page } from '@cloudflare/playwright';