You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

67 lines
2.0 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

import type { Browser, LaunchOptions } from "playwright";
export type BrowserLaunchOptions = {
headless: boolean;
slowMo?: number;
};
function readBrowserChannel(): "chrome" | "msedge" | undefined {
const raw = process.env.RPA_BROWSER_CHANNEL?.trim().toLowerCase();
if (raw === "chrome" || raw === "msedge") {
return raw;
}
return undefined;
}
function buildLaunchOptions(opts: BrowserLaunchOptions): LaunchOptions {
const channel = readBrowserChannel();
return {
headless: opts.headless,
...(opts.slowMo !== undefined ? { slowMo: opts.slowMo } : {}),
...(channel ? { channel } : {}),
args: [
"--disable-blink-features=AutomationControlled",
"--disable-dev-shm-usage",
"--disable-features=TranslateUI",
],
};
}
/** 启动浏览器stealth > patchright > playwright可选本地 Chrome channel */
export async function launchRpaBrowser(
opts: BrowserLaunchOptions,
): Promise<Browser> {
const launchOpts = buildLaunchOptions(opts);
if (process.env.RPA_USE_STEALTH === "true") {
try {
const { chromium } = await import("playwright-extra");
const StealthPlugin = (await import("puppeteer-extra-plugin-stealth"))
.default;
chromium.use(StealthPlugin());
return chromium.launch(launchOpts);
} catch (error) {
console.warn(
"[browser-launch] stealth 未安装或启动失败,回退默认 chromium",
error instanceof Error ? error.message : error,
);
}
}
if (process.env.RPA_USE_PATCHRIGHT === "true") {
try {
const patchright = (await import(
/* webpackIgnore: true */ "patchright" as string
)) as {
chromium: { launch: (o: LaunchOptions) => Promise<Browser> };
};
return patchright.chromium.launch(launchOpts);
} catch {
console.warn("[browser-launch] patchright 未安装,回退 playwright");
}
}
const { chromium } = await import("playwright");
return chromium.launch(launchOpts);
}