mirror of
https://github.com/the1812/Bilibili-Evolved.git
synced 2025-11-04 21:22:45 +08:00
Fix comments API bug
This commit is contained in:
parent
944c4d0208
commit
fc2928c89d
@ -8,6 +8,7 @@ export interface CommentItem {
|
||||
timeText: string
|
||||
likes: number
|
||||
replies: Omit<CommentItem, 'replies'>[]
|
||||
onRepliesUpdate?: (replies: Omit<CommentItem, 'replies'>[]) => void
|
||||
}
|
||||
export type CommentItemCallback = (item: CommentItem) => void
|
||||
export interface CommentArea {
|
||||
@ -22,6 +23,19 @@ export type CommentAreaCallback = (area: CommentArea) => void
|
||||
const commentAreaCallbacks: CommentAreaCallback[] = []
|
||||
const parseCommentItem = (element: HTMLElement) => {
|
||||
const user = element.querySelector('.con .user .name') as HTMLElement
|
||||
const parseReplyItem = (replyElement: HTMLElement) => {
|
||||
const replyFace = replyElement.querySelector('.reply-face') as HTMLElement
|
||||
const replyUser = replyElement.querySelector('.reply-con .user .name') as HTMLElement
|
||||
return {
|
||||
id: replyElement.getAttribute('data-id')!,
|
||||
element: replyElement,
|
||||
userID: replyFace.getAttribute('data-usercard-mid')!,
|
||||
userName: replyUser.textContent!,
|
||||
content: replyElement.querySelector('.text-con')!.textContent!,
|
||||
timeText: replyElement.querySelector('.info .time')!.textContent!,
|
||||
likes: parseInt(replyElement.querySelector('.info .like span')!.textContent!),
|
||||
}
|
||||
}
|
||||
const item: CommentItem = {
|
||||
id: element.getAttribute('data-id')!,
|
||||
element,
|
||||
@ -30,18 +44,18 @@ const parseCommentItem = (element: HTMLElement) => {
|
||||
content: element.querySelector('.con .text')!.textContent!,
|
||||
timeText: element.querySelector('.con .info .time')!.textContent!,
|
||||
likes: parseInt(element.querySelector('.con .like span')!.textContent!),
|
||||
replies: dqa(element, '.reply-box .reply-item').map((replyElement: HTMLElement) => {
|
||||
const replyUser = replyElement.querySelector('.reply-con .user') as HTMLElement
|
||||
return {
|
||||
id: replyElement.getAttribute('data-id')!,
|
||||
element: replyElement,
|
||||
userID: replyUser.getAttribute('data-usercard-mid')!,
|
||||
userName: replyUser.textContent!,
|
||||
content: replyElement.querySelector('.text-con')!.textContent!,
|
||||
timeText: replyElement.querySelector('.info .time')!.textContent!,
|
||||
likes: parseInt(replyElement.querySelector('.info .like span')!.textContent!),
|
||||
replies: [],
|
||||
}
|
||||
if (dq(element, '.reply-box .view-more')) {
|
||||
const replyBox = dq(element, '.reply-box') as HTMLElement
|
||||
Observer.childList(replyBox, records => {
|
||||
item.replies = dqa(element, '.reply-box .reply-item').map(parseReplyItem)
|
||||
if (records.length !== 0) {
|
||||
item.onRepliesUpdate && item.onRepliesUpdate(item.replies)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
item.replies = dqa(element, '.reply-box .reply-item').map(parseReplyItem)
|
||||
}
|
||||
return item
|
||||
}
|
||||
|
||||
@ -79,6 +79,7 @@ export const commentsTranslate = async () => {
|
||||
forEachCommentItem({
|
||||
added: (item) => {
|
||||
const { element } = item
|
||||
item.onRepliesUpdate = replies => replies.forEach(r => injectButton(r.element))
|
||||
injectButton(element)
|
||||
},
|
||||
})
|
||||
|
||||
@ -25,20 +25,24 @@ import { CommentItem } from '../comment-apis'
|
||||
return ''
|
||||
}
|
||||
const addCopyLinkButton = (comment: CommentItem) => {
|
||||
[comment, ...comment.replies].forEach(item => {
|
||||
const operationList = dq(item.element, '.opera-list ul') as HTMLUListElement
|
||||
if (!operationList || dq(operationList, '.copy-link')) {
|
||||
return
|
||||
}
|
||||
const copyLinkButton = document.createElement('li')
|
||||
copyLinkButton.classList.add('copy-link')
|
||||
copyLinkButton.textContent = '复制链接'
|
||||
copyLinkButton.addEventListener('click', () => {
|
||||
const url = findParentFeedsUrl(item.element) || document.URL
|
||||
GM_setClipboard(`${url}#reply${item.id}`, { mimetype: 'text/plain' })
|
||||
const processItems = (items: Omit<CommentItem, 'replies'>[]) => {
|
||||
items.forEach(item => {
|
||||
const operationList = dq(item.element, '.opera-list ul') as HTMLUListElement
|
||||
if (!operationList || dq(operationList, '.copy-link')) {
|
||||
return
|
||||
}
|
||||
const copyLinkButton = document.createElement('li')
|
||||
copyLinkButton.classList.add('copy-link')
|
||||
copyLinkButton.textContent = '复制链接'
|
||||
copyLinkButton.addEventListener('click', () => {
|
||||
const url = findParentFeedsUrl(item.element) || document.URL
|
||||
GM_setClipboard(`${url}#reply${item.id}`, { mimetype: 'text/plain' })
|
||||
})
|
||||
operationList.appendChild(copyLinkButton)
|
||||
})
|
||||
operationList.appendChild(copyLinkButton)
|
||||
})
|
||||
}
|
||||
processItems([comment, ...comment.replies])
|
||||
comment.onRepliesUpdate = replies => processItems(replies)
|
||||
}
|
||||
forEachCommentItem({
|
||||
added: addCopyLinkButton
|
||||
|
||||
Loading…
Reference in New Issue
Block a user