mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
fix(active-video-links): nicovideo url fix
This commit is contained in:
parent
b3189a328c
commit
6e8780a9c2
@ -34,31 +34,68 @@ const processDescLinks = () => {
|
|||||||
if (!descContainer) {
|
if (!descContainer) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
const walker = document.createTreeWalker(descContainer, NodeFilter.SHOW_TEXT, {
|
||||||
const content = descContainer.innerHTML
|
acceptNode: node => {
|
||||||
let newContent = content
|
if (node.parentElement?.closest('a')) return NodeFilter.FILTER_REJECT
|
||||||
|
return webRegex.test(node.textContent || '') ? NodeFilter.FILTER_ACCEPT : NodeFilter.FILTER_REJECT
|
||||||
const matches = [...content.matchAll(webRegex)]
|
|
||||||
for (let i = matches.length - 1; i >= 0; i--) {
|
|
||||||
const match = matches[i]
|
|
||||||
const matchText = match[0]
|
|
||||||
const startIndex = match.index ?? 0
|
|
||||||
|
|
||||||
if (isTextInsideLink(content, startIndex)) {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
|
})
|
||||||
const link = matchText.replace(/^https?:\/\//, '//').replace(/^www\./, '//')
|
const textNodes: Text[] = []
|
||||||
const replacement = `<a href='${link}' target='_blank'>${matchText}</a>`
|
let currentNode: Node | null
|
||||||
|
while ((currentNode = walker.nextNode())) {
|
||||||
newContent =
|
textNodes.push(currentNode as Text)
|
||||||
newContent.substring(0, startIndex) +
|
|
||||||
replacement +
|
|
||||||
newContent.substring(startIndex + matchText.length)
|
|
||||||
}
|
}
|
||||||
|
textNodes.forEach(textNode => {
|
||||||
|
const frag = document.createDocumentFragment()
|
||||||
|
const text = textNode.textContent || ''
|
||||||
|
let lastIndex = 0
|
||||||
|
let match: RegExpExecArray | null
|
||||||
|
webRegex.lastIndex = 0
|
||||||
|
while ((match = webRegex.exec(text)) !== null) {
|
||||||
|
const start = match.index
|
||||||
|
if (lastIndex < start) {
|
||||||
|
frag.appendChild(document.createTextNode(text.slice(lastIndex, start)))
|
||||||
|
}
|
||||||
|
const urlText = match[0]
|
||||||
|
const linkHref = urlText.replace(/^https?:\/\//, '//').replace(/^www\./, '//')
|
||||||
|
const a = document.createElement('a')
|
||||||
|
a.href = linkHref
|
||||||
|
a.target = '_blank'
|
||||||
|
a.textContent = urlText
|
||||||
|
frag.appendChild(a)
|
||||||
|
lastIndex = start + urlText.length
|
||||||
|
}
|
||||||
|
if (lastIndex < text.length) {
|
||||||
|
frag.appendChild(document.createTextNode(text.slice(lastIndex)))
|
||||||
|
}
|
||||||
|
textNode.parentNode?.replaceChild(frag, textNode)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
if (newContent !== content) {
|
const normalizeNicoDescLinks = () => {
|
||||||
descContainer.innerHTML = newContent
|
const descContainer = document.querySelector('.desc-info-text')
|
||||||
|
if (!descContainer) return
|
||||||
|
const anchors = Array.from(descContainer.querySelectorAll('a'))
|
||||||
|
for (let i = 0; i < anchors.length - 1; i++) {
|
||||||
|
const a1 = anchors[i]
|
||||||
|
const a2 = anchors[i + 1]
|
||||||
|
const href1 = a1.getAttribute('href') || ''
|
||||||
|
const text2 = a2.textContent?.trim() || ''
|
||||||
|
const href2 = a2.getAttribute('href') || ''
|
||||||
|
// first link is base watch URL, second has sm ID text
|
||||||
|
if (/^(https?:)?\/\/(www\.)?nicovideo\.jp\/watch\/?$/.test(href1)
|
||||||
|
&& /^sm\d+$/.test(text2)
|
||||||
|
&& href2.includes(`/watch/${text2}`)) {
|
||||||
|
const newHref = href2.replace(/^(https?:)?/, '//')
|
||||||
|
const newA = document.createElement('a')
|
||||||
|
newA.href = newHref
|
||||||
|
newA.target = '_blank'
|
||||||
|
newA.textContent = text2
|
||||||
|
a1.replaceWith(newA)
|
||||||
|
a2.remove()
|
||||||
|
console.log(`Merged Nico link: ${text2}`)
|
||||||
|
i++
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -66,6 +103,7 @@ const processLinks = () => {
|
|||||||
try {
|
try {
|
||||||
processAcgLinks()
|
processAcgLinks()
|
||||||
processDescLinks()
|
processDescLinks()
|
||||||
|
normalizeNicoDescLinks()
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('处理链接时遇到 Error:', error)
|
console.error('处理链接时遇到 Error:', error)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user