fix(active-video-links): nicovideo url fix

This commit is contained in:
Alan Ye 2025-04-27 22:31:12 +08:00
parent b3189a328c
commit 6e8780a9c2
No known key found for this signature in database

View File

@ -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 textNodes: Text[] = []
const matchText = match[0] let currentNode: Node | null
const startIndex = match.index ?? 0 while ((currentNode = walker.nextNode())) {
textNodes.push(currentNode as Text)
if (isTextInsideLink(content, startIndex)) { }
continue 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)
})
} }
const link = matchText.replace(/^https?:\/\//, '//').replace(/^www\./, '//') const normalizeNicoDescLinks = () => {
const replacement = `<a href='${link}' target='_blank'>${matchText}</a>` const descContainer = document.querySelector('.desc-info-text')
if (!descContainer) return
newContent = const anchors = Array.from(descContainer.querySelectorAll('a'))
newContent.substring(0, startIndex) + for (let i = 0; i < anchors.length - 1; i++) {
replacement + const a1 = anchors[i]
newContent.substring(startIndex + matchText.length) 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++
} }
if (newContent !== content) {
descContainer.innerHTML = newContent
} }
} }
@ -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)
} }