| 6 | * Cross-origin stylesheets are flagged for server-side fetching. |
| 7 | */ |
| 8 | export async function extractSourceCSS(page: Page): Promise<string[]> { |
| 9 | return page.evaluate(() => { |
| 10 | const sheets: string[] = []; |
| 11 | |
| 12 | // 1. Inline <style> tags |
| 13 | document.querySelectorAll('style').forEach((el) => { |
| 14 | if (el.textContent) { |
| 15 | sheets.push(el.textContent); |
| 16 | } |
| 17 | }); |
| 18 | |
| 19 | // 2. Linked stylesheets (same-origin only — cross-origin will throw) |
| 20 | for (const sheet of document.styleSheets) { |
| 21 | try { |
| 22 | const rules = Array.from(sheet.cssRules).map((r) => r.cssText); |
| 23 | sheets.push(rules.join('\n')); |
| 24 | } catch { |
| 25 | // Cross-origin stylesheet — log the URL so we can fetch it server-side |
| 26 | if (sheet.href) { |
| 27 | sheets.push(`/* CROSS-ORIGIN: ${sheet.href} */`); |
| 28 | } |
| 29 | } |
| 30 | } |
| 31 | |
| 32 | return sheets; |
| 33 | }); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Fetch cross-origin stylesheets that the browser couldn't read due to CORS. |