File size: 1,399 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
31
32
33
34
35
36
37
38
39
40
41
42
import { Page } from "playwright";

export async function extractStatistics(page: Page): Promise<{ value: string; context: string }[]> {
  const result = await page.evaluate(`
    (() => {
      const clean = (s) => s.replace(/\\s+/g, " ").trim();
      const getText = (el) => el.innerText || el.textContent || "";

      const results = [];
      const elements = Array.from(document.querySelectorAll("*"));

      for (const el of elements) {
        const text = clean(getText(el));
        if (text.length < 10 || text.length > 120) continue;

        const childText = clean(
          Array.from(el.children).map((c) => getText(c)).join(" ")
        );
        if (childText.length > 0 && Math.abs(childText.length - text.length) < 5) continue;

        if (/checkout|cart|card number|email|tax|total|loyalty/i.test(text)) continue;

        const match = text.match(/(\\$?\\d+(?:\\.\\d+)?(?:,\\d{3})*(?:[MBKT%+]))/);
        if (!match) continue;

        const value = match[1];
        if (value === "10%" || /\\d+\\.\\d+%/.test(value) || value.includes("1.667")) continue;

        results.push({ value, context: text });
      }

      const seen = new Set();
      return results.filter((x) => {
        if (seen.has(x.value)) return false;
        seen.add(x.value);
        return true;
      }).slice(0, 20);
    })()
  `);
  return result as { value: string; context: string }[];
}