From 7763f34d5d07a104f8a66e53b05a7f87a4e0da28 Mon Sep 17 00:00:00 2001 From: molvqingtai Date: Thu, 14 Nov 2024 22:44:03 +0800 Subject: [PATCH] fix: parse icon url error --- src/utils/getSiteInfo.ts | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/src/utils/getSiteInfo.ts b/src/utils/getSiteInfo.ts index d32b9ba..a5c7651 100644 --- a/src/utils/getSiteInfo.ts +++ b/src/utils/getSiteInfo.ts @@ -10,6 +10,21 @@ export interface SiteInfo { description: string } +const getIcon = (): string => { + const path = + document.querySelector('link[rel="icon" i]')?.getAttribute('href') ?? + document.querySelector('link[rel="shortcut icon" i]')?.getAttribute('href') ?? + document.querySelector('meta[property="og:image" i]')?.getAttribute('content') ?? + document.querySelector('link[rel="apple-touch-icon" i]')?.getAttribute('href') ?? + `/favicon.ico` + + if (path.startsWith('data:') || path.startsWith('//')) { + return path + } else { + return buildFullURL(document.location.origin, path) + } +} + const getSiteInfo = (): SiteInfo => { return { host: document.location.host, @@ -20,14 +35,7 @@ const getSiteInfo = (): SiteInfo => { document.querySelector('meta[property="og:site_name" i]')?.getAttribute('content') ?? document.querySelector('meta[property="og:title" i]')?.getAttribute('content') ?? document.title, - icon: buildFullURL( - document.location.origin, - document.querySelector('link[rel="icon" i]')?.getAttribute('href') ?? - document.querySelector('link[rel="shortcut icon" i]')?.getAttribute('href') ?? - document.querySelector('meta[property="og:image" i]')?.getAttribute('content') ?? - document.querySelector('link[rel="apple-touch-icon" i]')?.getAttribute('href') ?? - `/favicon.ico` - ), + icon: getIcon(), description: document.querySelector('meta[property="og:description i"]')?.getAttribute('content') ?? document.querySelector('meta[name="description" i]')?.getAttribute('content') ??