fix: parse icon url error

This commit is contained in:
molvqingtai 2024-11-14 22:44:03 +08:00
parent d0fea9e42d
commit 7763f34d5d

View file

@ -10,6 +10,21 @@ export interface SiteInfo {
description: string 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 => { const getSiteInfo = (): SiteInfo => {
return { return {
host: document.location.host, 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:site_name" i]')?.getAttribute('content') ??
document.querySelector('meta[property="og:title" i]')?.getAttribute('content') ?? document.querySelector('meta[property="og:title" i]')?.getAttribute('content') ??
document.title, document.title,
icon: buildFullURL( icon: getIcon(),
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`
),
description: description:
document.querySelector('meta[property="og:description i"]')?.getAttribute('content') ?? document.querySelector('meta[property="og:description i"]')?.getAttribute('content') ??
document.querySelector('meta[name="description" i]')?.getAttribute('content') ?? document.querySelector('meta[name="description" i]')?.getAttribute('content') ??