website-builde / lib /statistics.ts
Daviddolor's picture
Single-process Chromium, fixed visual-polish prompt, Docker config for HF Space
ec675f2
Raw
History Blame Contribute Delete
1.4 kB
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 }[];
}