File size: 1,080 Bytes
ec675f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import { Browser } from "playwright";
import * as path from "path";
import { extractPageData } from "./extract";
import { extractStatistics } from "./statistics";

export async function crawlPage(browser: Browser, url: string) {
  const context = await browser.newContext({ viewport: { width: 1440, height: 900 } });

  try {
    const page = await context.newPage();

    await page.goto(url, { waitUntil: "domcontentloaded", timeout: 60000 });
    await page.waitForTimeout(2000);

    const hostname = new URL(url).hostname.replace(/[^a-z0-9.-]/gi, "");
    const screenshotPath = path.join("/tmp", `crawl-${hostname}-${Date.now()}.png`);
    await page.screenshot({ path: screenshotPath, fullPage: true });

    const data = await extractPageData(page);
    const statistics = await extractStatistics(page);

    const vp = page.viewportSize();
    const viewport = vp ? `${vp.width}x${vp.height}` : "unknown";

    return { url, screenshot: screenshotPath, viewport, ...data, statistics };
  } finally {
    await context.close(); // always runs — success or failure
  }
}