Add comment apis

This commit is contained in:
the1812 2020-07-19 15:51:16 +08:00
parent a9c46ce849
commit d267b7d7e6
9 changed files with 137 additions and 14 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -27,7 +27,7 @@
"combo-like.min.js": "368DA34768E947628099D258ED23BC5FFFCFFD25A47346E086C7975C8A52F997",
"comment.min.css": "78C167978B3F1351AB8F8F9B39B47AB178DE2A775CE63A83F2287BEB02059673",
"comment.min.js": "64F7B6951861C25799C48BB1ABCF2A73F79BCBBFC1E9D3BD45EC1F16F94F6451",
"comment-apis.min.js": "13B755C0EE0CA018AE65251E168BAF3395FEA2EB4FAD6949AADD752F61B8819B",
"comment-apis.min.js": "E6B1B2DE0961F85E903504010FDCF6AFEB91E4E8B5CB07C7CAE7AE479ECE9427",
"comment-dark.min.css": "E980508E86203743C36FEE4F7149BFF53961ECBC4EC140E8C162D18D395B940A",
"compact-layout.min.css": "CAC8B0DBA8E90B38D31F0811B7B469709052204AC36D5D3F20FE5D0899FDDCCB",
"compact-layout.min.js": "B20609A7CBBDB1845FA0156FB5BE6B1E1A1B8B069EA85F65D16241DD2C12D738",
@ -58,7 +58,7 @@
"danmaku-send-bar.min.css": "52149D46CF3A15CF6EB186CB49F298DE18B0723E52D8693228F66099847EDCCB",
"danmaku-send-bar.min.js": "7108E72F00C169F26E20409F4AA79F05553310375BABC392EB7E2166224B800A",
"dark.min.css": "53FC0A641292561F4F453D3FD83C37616B513D36C0974AAE167897D8658FCE3E",
"dark.user.css": "5C5678B5C306F33D728F89435667E37E1F8F7274BDB29FCA83B7B8BFA5500C50",
"dark.user.css": "AEEF10A76EDD1AE248EDE7E2050B65BDDF3BC4BA5C57206CDAB2E6D8BF11B106",
"dark-color-scheme.min.js": "6D353AC738180317ACF9D2E046BFFBA0ACC0FA7A8D22B8E16C51127CDE4FE96B",
"dark-important.min.css": "5369385A2C16188FB11E71C4C5B91275646F22F0F09E518E37813CC4FA23A072",
"dark-navbar.min.css": "7FAD547E326B768904B3739C978DF86FFDDD5E5064FB8F490710D197CCC3018B",

Binary file not shown.

View File

@ -1 +1 @@
(()=>{return(r,e)=>{}})();
(()=>{return(e,t)=>{const n="bb-comment";const r=[];const o=[];const s=[];const c=[];const i=e=>{const t=e.querySelector(".con .user .name");const n={id:e.getAttribute("data-id"),element:e,userID:t.getAttribute("data-usercard-mid"),userName:t.textContent,content:e.querySelector(".con .text").textContent,timeText:e.querySelector(".con .info .time").textContent,likes:parseInt(e.querySelector(".con .like span").textContent),replies:dqa(e,".reply-box .reply-item").map(e=>{const t=e.querySelector(".reply-con .user");return{id:e.getAttribute("data-id"),element:e,userID:t.getAttribute("data-usercard-mid"),userName:t.textContent,content:e.querySelector(".text-con").textContent,timeText:e.querySelector(".info .time").textContent,likes:parseInt(e.querySelector(".info .like span").textContent)}})};return n};const a=e=>{if(e.observer){return}e.items=dqa(e.element,".list-item.reply-wrap").map(i);e.items.forEach(e=>{o.forEach(t=>t(e))});e.observer=Observer.childListSubtree(e.element,t=>{t.forEach(t=>{const n=e=>{return e instanceof HTMLElement&&e.classList.contains("list-item")&&e.classList.contains("reply-wrap")};t.addedNodes.forEach(t=>{if(n(t)){const n=i(t);e.items.push(n);o.forEach(e=>e(n))}});t.removedNodes.forEach(t=>{if(n(t)){const n=t.getAttribute("data-id");const r=e.items.findIndex(e=>e.id===n);if(r!==-1){const[t]=e.items.splice(r,1);s.forEach(e=>e(t))}}})})})};const m=e=>{if(e instanceof HTMLElement&&e.classList.contains(n)){const t={element:e,items:[]};r.push(t);a(t);c.forEach(e=>e(t))}};const d=e=>{r.forEach(t=>e(t));c.push(e)};const u=e=>{const{added:t,removed:n}=e;r.forEach(e=>{e.items.forEach(e=>t(e));o.push(t);s.push(n)})};Observer.childListSubtree(document.body,e=>{e.forEach(e=>{e.addedNodes.forEach(e=>m(e))})});dqa("."+n).forEach(m);return{export:{commentAreas:r,forEachCommentArea:d,forEachCommentItem:u}}}})();

View File

@ -3,7 +3,7 @@
@namespace Bilibili-Evolved
@homepageURL https://github.com/the1812/Bilibili-Evolved
@updateURL https://cdn.jsdelivr.net/gh/the1812/Bilibili-Evolved@preview/min/dark.user.css
@version 733.79.0
@version 734.01.0
@license MIT
@author Grant Howard (https://github.com/the1812), Coulomb-G (https://github.com/Coulomb-G)
==/UserStyle== */

View File

@ -0,0 +1,123 @@
const commentAreaClass = 'bb-comment'
export interface CommentItem {
element: HTMLElement
id: string
userID: string
userName: string
content: string
timeText: string
likes: number
replies: Omit<CommentItem, 'replies'>[]
}
export type CommentItemCallback = (item: CommentItem) => void
export interface CommentArea {
element: HTMLElement
items: CommentItem[]
observer?: Observer
}
export const commentAreas: CommentArea[] = []
const itemAddedCallbacks: CommentItemCallback[] = []
const itemRemovedCallbacks: CommentItemCallback[] = []
export type CommentAreaCallback = (area: CommentArea) => void
const commentAreaCallbacks: CommentAreaCallback[] = []
const parseCommentItem = (element: HTMLElement) => {
const user = element.querySelector('.con .user .name') as HTMLElement
const item: CommentItem = {
id: element.getAttribute('data-id')!,
element,
userID: user.getAttribute('data-usercard-mid')!,
userName: user.textContent!,
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!),
}
})
}
return item
}
const observeItems = (area: CommentArea) => {
if (area.observer) {
return
}
area.items = dqa(area.element, '.list-item.reply-wrap').map(parseCommentItem)
area.items.forEach(item => {
itemAddedCallbacks.forEach(c => c(item))
})
area.observer = Observer.childListSubtree(area.element, records => {
records.forEach(r => {
const isCommentItem = (n: Node): n is HTMLElement => {
return n instanceof HTMLElement &&
n.classList.contains('list-item') &&
n.classList.contains('reply-wrap')
}
r.addedNodes.forEach(n => {
if (isCommentItem(n)) {
const commentItem = parseCommentItem(n)
area.items.push(commentItem)
itemAddedCallbacks.forEach(c => c(commentItem))
}
})
r.removedNodes.forEach(n => {
if (isCommentItem(n)) {
const id = n.getAttribute('data-id')!
const index = area.items.findIndex(item => item.id === id)
if (index !== -1) {
const [commentItem] = area.items.splice(index, 1)
itemRemovedCallbacks.forEach(c => c(commentItem))
}
}
})
})
})
}
const observeAreas = (node: Node) => {
if (node instanceof HTMLElement && node.classList.contains(commentAreaClass)) {
const area: CommentArea = {
element: node,
items: [],
}
commentAreas.push(area)
observeItems(area)
commentAreaCallbacks.forEach(c => c(area))
}
}
export const forEachCommentArea = (callback: CommentAreaCallback) => {
commentAreas.forEach(it => callback(it))
commentAreaCallbacks.push(callback)
}
export const forEachCommentItem = (callbacks: {
added: CommentItemCallback
removed: CommentItemCallback
}) => {
const { added, removed } = callbacks
commentAreas.forEach(area => {
area.items.forEach(item => added(item))
itemAddedCallbacks.push(added)
itemRemovedCallbacks.push(removed)
})
}
Observer.childListSubtree(document.body, records => {
records.forEach(r => {
r.addedNodes.forEach(n => observeAreas(n))
})
})
dqa('.' + commentAreaClass).forEach(observeAreas)
export default {
export: {
commentAreas,
forEachCommentArea,
forEachCommentItem,
}
}