MCPcopy Create free account
hub / github.com/ZToolsCenter/ZTools / parseFaviconFromHtml

Method parseFaviconFromHtml

src/main/api/renderer/webSearch.ts:395–416  ·  view source on GitHub ↗

* 从 HTML 中解析 favicon URL

(html: string, origin: string)

Source from the content-addressed store, hash-verified

393 * 从 HTML 中解析 favicon URL
394 */
395 private parseFaviconFromHtml(html: string, origin: string): string {
396 // 匹配 <link rel="icon" href="..."> 或 <link rel="shortcut icon" href="...">
397 const linkRegex = /<link[^>]*rel=["'](?:shortcut\s+)?icon["'][^>]*href=["']([^"']+)["'][^>]*>/gi
398 const altRegex = /<link[^>]*href=["']([^"']+)["'][^>]*rel=["'](?:shortcut\s+)?icon["'][^>]*>/gi
399
400 const match = linkRegex.exec(html) || altRegex.exec(html)
401 if (match?.[1]) {
402 const href = match[1]
403 // 处理相对路径
404 if (href.startsWith('//')) {
405 return `https:${href}`
406 } else if (href.startsWith('/')) {
407 return `${origin}${href}`
408 } else if (href.startsWith('http')) {
409 return href
410 } else {
411 return `${origin}/${href}`
412 }
413 }
414
415 return ''
416 }
417
418 /**
419 * HTTP GET 请求,返回文本内容

Callers 1

fetchFaviconMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected