add link title

pull/8/head
Septs 2023-02-15 12:42:55 +08:00
parent ec813570f2
commit a2b18fa7e6
No known key found for this signature in database
GPG Key ID: 65144037AFA516E1
1 changed files with 25 additions and 0 deletions

View File

@ -13,6 +13,7 @@ Promise.resolve()
.then(onRestoreEmailAddress)
.then(onReferenceLinks)
.then(onAbbreviation)
.then(onLinkTitle)
function onTableStyle() {
const content = document.querySelector('article.content')
@ -113,6 +114,30 @@ function onAbbreviation() {
})
}
function onLinkTitle() {
for (const link of document.links) {
if (link.title) continue
let title = getTitle(link as HTMLAnchorElement)
if (!title) continue
title = decodeURIComponent(title)
if (link.textContent === title) continue
link.title = title
}
function getTitle(link: HTMLAnchorElement) {
const host = link.host
const path = link.pathname
let title: string | undefined
if (host === 'doi.org') return path.slice(1)
if (host === 'files.transfemscience.org' && path.startsWith('/pdfs/')) return path.slice(6)
if (host.endsWith('wikipedia.org') && path.startsWith('/wiki/')) {
title = path.slice(path.lastIndexOf('/') + 1)
if (link.hash) title = link.hash.slice(1) + ' on ' + link.title
return title.replace(/_/g, ' ')
}
}
}
function onImageFallback() {
document.addEventListener('load', onLoad, { capture: true })
document.addEventListener('error', onError, { capture: true })