(url: URL)
| 1 | import { JSDOM } from "jsdom"; |
| 2 | |
| 3 | export async function findFaviconPath(url: URL): Promise<string | undefined> { |
| 4 | const baseUrl = `${url.protocol}//${url.hostname}`; |
| 5 | |
| 6 | try { |
| 7 | const response = await fetch(baseUrl); |
| 8 | if (!response.ok) { |
| 9 | throw new Error(`HTTP error! status: ${response.status}`); |
| 10 | } |
| 11 | |
| 12 | const html = await response.text(); |
| 13 | const dom = new JSDOM(html); |
| 14 | const document = dom.window.document; |
| 15 | |
| 16 | // Look for favicon in <link> tags |
| 17 | const linkTags = document.querySelectorAll('link[rel*="icon"]'); |
| 18 | |
| 19 | for (const link of linkTags) { |
| 20 | const href = link.getAttribute("href"); |
| 21 | if (href) { |
| 22 | return new URL(href, baseUrl).toString(); |
| 23 | } |
| 24 | } |
| 25 | } catch (error) { |
| 26 | console.debug(`Failed to fetch favicon for ${baseUrl}: ${error}`); |
| 27 | } |
| 28 | |
| 29 | console.debug(`Failed to find favicon for ${baseUrl}`); |
| 30 | return undefined; |
| 31 | } |
| 32 | |
| 33 | export async function getFaviconBase64( |
| 34 | faviconUrl: string, |
no test coverage detected