fix: remove stealth plugin to eliminate rimraf crash, use plain puppeteer
This commit is contained in:
1 parent
7137b046d7
commit
cef068ff3c
1 file changed
+7
-16
@@ -1,17 +1,15 @@
|
||||
import puppeteer from "puppeteer";
|
||||
|
||||
const UA =
|
||||
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) " +
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) " +
|
||||
"Chrome/125.0.0.0 Safari/537.36";
|
||||
|
||||
let browser: any = null;
|
||||
let browser: Awaited<ReturnType<typeof puppeteer.launch>> | null = null;
|
||||
|
||||
async function getBrowser() {
|
||||
if (browser) return browser;
|
||||
try {
|
||||
const puppeteer = await import("puppeteer-extra");
|
||||
const StealthPlugin = await import("puppeteer-extra-plugin-stealth");
|
||||
puppeteer.default.use(StealthPlugin.default());
|
||||
browser = await puppeteer.default.launch({
|
||||
browser = await puppeteer.launch({
|
||||
headless: true,
|
||||
args: [
|
||||
"--no-sandbox",
|
||||
@@ -22,23 +20,18 @@ async function getBrowser() {
|
||||
],
|
||||
});
|
||||
return browser;
|
||||
} catch (e) {
|
||||
throw new Error(`Cloudflare bypass unavailable: ${(e as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchWithCfBypass(
|
||||
url: string,
|
||||
timeout = 30000,
|
||||
): Promise<string> {
|
||||
let b: any;
|
||||
let page: any;
|
||||
try {
|
||||
b = await getBrowser();
|
||||
page = await b.newPage();
|
||||
const b = await getBrowser();
|
||||
const page = await b.newPage();
|
||||
await page.setUserAgent(UA);
|
||||
await page.setDefaultTimeout(timeout);
|
||||
|
||||
try {
|
||||
const response = await page.goto(url, {
|
||||
waitUntil: "networkidle2",
|
||||
timeout,
|
||||
@@ -72,7 +65,6 @@ export async function fetchWithCfBypass(
|
||||
const finalBody = await page.content();
|
||||
return finalBody;
|
||||
} finally {
|
||||
if (page) {
|
||||
try {
|
||||
await page.close();
|
||||
} catch {
|
||||
@@ -80,7 +72,6 @@ export async function fetchWithCfBypass(
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export async function fetchJsonWithCfBypass<T = unknown>(
|
||||
url: string,
|
||||
|
||||
Reference in new issue
Block a user