headless now also returns network requests

This commit is contained in:
2025-12-04 15:39:25 +01:00
parent d0cc461c1c
commit d2eb2b9946

View File

@@ -31,7 +31,6 @@ async function initHeadless() {
"--no-first-run", "--no-first-run",
"--no-zygote", "--no-zygote",
"--single-process", "--single-process",
"--disable-software-rasterizer", "--disable-software-rasterizer",
"--disable-client-side-phishing-detection", "--disable-client-side-phishing-detection",
"--no-default-browser-check", "--no-default-browser-check",
@@ -85,6 +84,15 @@ async function scrape(url, handler, options = {}) {
if (!browser) await initHeadless(); if (!browser) await initHeadless();
const page = await context.newPage(); const page = await context.newPage();
const requests = [];
page.on("request", req => {
requests.push({
url: req.url(),
method: req.method(),
type: req.resourceType()
});
});
await page.route("**/*", (route) => { await page.route("**/*", (route) => {
const req = route.request(); const req = route.request();
@@ -137,8 +145,8 @@ async function scrape(url, handler, options = {}) {
await new Promise(r => setTimeout(r, renderWaitTime)); await new Promise(r => setTimeout(r, renderWaitTime));
} }
return await handler(page); const result = await handler(page);
return { result, requests };
} catch (error) { } catch (error) {
console.error(`Error durante el scraping de ${url}:`, error); console.error(`Error durante el scraping de ${url}:`, error);
return null; return null;