Skip to content

Commit 0f596d0

Browse files
committed
实现“加载多页回复”
1 parent b265fa7 commit 0f596d0

File tree

5 files changed

+108
-62
lines changed

5 files changed

+108
-62
lines changed

src/contents/globals.ts

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import type { CommentData } from '../types'
2-
31
/** 登录人的昵称 */
42
export const loginName = $('#Top .tools > a[href^="/member"]').text()
53

@@ -17,9 +15,14 @@ export const $topicContentBox = $('#Main .box:has(.topic_content)')
1715
export const $commentBox = $('#Main .box:has(.cell[id^="r_"])')
1816

1917
/** 评论区的回复 */
20-
export const $commentCells = $commentBox.find('.cell[id^="r_"]')
18+
export let $commentCells = $commentBox.find('.cell[id^="r_"]')
19+
20+
export let $commentTableRows = $commentCells.find('> table > tbody > tr')
2121

22-
export const $commentTableRows = $commentCells.find('> table > tbody > tr')
22+
export function updateCommentCells() {
23+
$commentCells = $commentBox.find('.cell[id^="r_"]')
24+
$commentTableRows = $commentCells.find('> table > tbody > tr')
25+
}
2326

2427
/** 回复输入控件 */
2528
export const $replyBox = $('#reply-box')
@@ -31,54 +34,3 @@ export const colorTheme = $('#Wrapper').hasClass('Night') ? 'dark' : 'light'
3134
/** 主题回复输入框 */
3235
export const $replyTextArea = $('#reply_content')
3336
export const replyTextArea = document.querySelector('#reply_content')
34-
35-
/** 每一页的回复列表数据 */
36-
export const commentDataList: readonly CommentData[] = $commentTableRows
37-
.map<CommentData>((idx, tr) => {
38-
const id = $commentCells[idx].id
39-
40-
const $tr = $(tr)
41-
const $td = $tr.find('> td:nth-child(3)')
42-
43-
const thanked = $tr.find('> td:last-of-type > .fr').find('> .thank_area').hasClass('thanked')
44-
45-
const $member = $td.find('> strong > a')
46-
const memberName = $member.text()
47-
const memberLink = $member.prop('href')
48-
const memberAvatar = $tr.find('.avatar').prop('src')
49-
50-
const content = $td.find('> .reply_content').text()
51-
const likes = Number($td.find('span.small').text())
52-
const floor = $td.find('span.no').text()
53-
54-
const memberNameMatches = Array.from(content.matchAll(/@([a-zA-Z0-9]+)/g))
55-
const refMemberNames =
56-
memberNameMatches.length > 0
57-
? memberNameMatches.map(([, name]) => {
58-
return name
59-
})
60-
: undefined
61-
62-
const floorNumberMatches = Array.from(content.matchAll(/#(\d+)/g))
63-
const refFloors =
64-
floorNumberMatches.length > 0
65-
? floorNumberMatches.map(([, floor]) => {
66-
return floor
67-
})
68-
: undefined
69-
70-
return {
71-
id,
72-
memberName,
73-
memberLink,
74-
memberAvatar,
75-
content,
76-
likes,
77-
floor,
78-
index: idx,
79-
refMemberNames,
80-
refFloors,
81-
thanked,
82-
}
83-
})
84-
.get()

src/contents/topic/comment.ts

Lines changed: 89 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,25 @@ import { createPopup } from '../../components/popup'
33
import { createToast } from '../../components/toast'
44
import { StorageKey } from '../../constants'
55
import { iconHeart, iconHide, iconReply } from '../../icons'
6-
import type { Member } from '../../types'
6+
import { fetchTopicPage } from '../../services'
7+
import type { CommentData, Member } from '../../types'
78
import { escapeHTML, getStorageSync } from '../../utils'
89
import {
910
$commentBox,
1011
$commentCells,
1112
$commentTableRows,
1213
$replyTextArea,
13-
commentDataList,
1414
loginName,
1515
topicOwnerName,
16+
updateCommentCells,
1617
} from '../globals'
1718
import { insertTextToReplyInput } from '../helpers'
1819
import { processAvatar } from './avatar'
1920
import { openTagsSetter, processReplyContent, updateMemberTag } from './content'
2021

22+
/** 每一页的回复列表数据 */
23+
let commentDataList: readonly CommentData[] = []
24+
2125
/**
2226
* 设置热门回复。
2327
*/
@@ -199,7 +203,89 @@ function handlingControls() {
199203
})
200204
}
201205

202-
export function handlingComments() {
206+
export async function handlingComments() {
207+
if (false) {
208+
const $paging = $('.v2p-paging')
209+
const $pagingTop = $paging.eq(0)
210+
const $pagingBottom = $paging.eq(1)
211+
212+
const $pageCurrent = $pagingTop.find('.page_current')
213+
const currentPage = $pageCurrent.text()
214+
215+
if (currentPage === '1') {
216+
const $pageNormal = $pagingTop.find('.page_normal')
217+
const pages: string[] = []
218+
$pageNormal.each((_, ele) => {
219+
if (ele.textContent) {
220+
ele.classList.add('page_current')
221+
pages.push(ele.textContent)
222+
}
223+
})
224+
225+
if (pages.length > 0) {
226+
const pagesText = await Promise.all(
227+
pages.map((p) => fetchTopicPage(window.location.pathname, p))
228+
)
229+
230+
pagesText.map((pageText) => {
231+
$pagingBottom.before($(pageText).find('.cell[id^="r_"]'))
232+
})
233+
}
234+
235+
updateCommentCells()
236+
}
237+
}
238+
239+
commentDataList = $commentTableRows
240+
.map<CommentData>((idx, tr) => {
241+
const id = $commentCells[idx].id
242+
243+
const $tr = $(tr)
244+
const $td = $tr.find('> td:nth-child(3)')
245+
246+
const thanked = $tr.find('> td:last-of-type > .fr').find('> .thank_area').hasClass('thanked')
247+
248+
const $member = $td.find('> strong > a')
249+
const memberName = $member.text()
250+
const memberLink = $member.prop('href')
251+
const memberAvatar = $tr.find('.avatar').prop('src')
252+
253+
const content = $td.find('> .reply_content').text()
254+
const likes = Number($td.find('span.small').text())
255+
const floor = $td.find('span.no').text()
256+
257+
const memberNameMatches = Array.from(content.matchAll(/@([a-zA-Z0-9]+)/g))
258+
const refMemberNames =
259+
memberNameMatches.length > 0
260+
? memberNameMatches.map(([, name]) => {
261+
return name
262+
})
263+
: undefined
264+
265+
const floorNumberMatches = Array.from(content.matchAll(/#(\d+)/g))
266+
const refFloors =
267+
floorNumberMatches.length > 0
268+
? floorNumberMatches.map(([, floor]) => {
269+
return floor
270+
})
271+
: undefined
272+
273+
return {
274+
id,
275+
memberName,
276+
memberLink,
277+
memberAvatar,
278+
content,
279+
likes,
280+
floor,
281+
index: idx,
282+
refMemberNames,
283+
refFloors,
284+
thanked,
285+
}
286+
})
287+
.get()
288+
203289
const storage = getStorageSync()
204290

205291
const tagData = storage[StorageKey.MemberTag]

src/contents/topic/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ void (async () => {
8282
}
8383
}
8484

85-
handlingComments()
8685
handlingPaging()
86+
void handlingComments()
8787
handleReply()
8888
})()

src/contents/topic/paging.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import { $commentBox } from '../globals'
22

33
export function handlingPaging() {
4-
const notCommentCells = $commentBox.find('> .cell:not([id^="r_"])')
4+
const $notCommentCells = $commentBox.find('> .cell:not([id^="r_"])')
55

6-
if (notCommentCells.length <= 1) {
6+
if ($notCommentCells.length <= 1) {
77
return
88
}
99

10-
const pagingCells = notCommentCells.slice(1).addClass('v2p-paging')
10+
// $notCommentCells 的第一个为 “xxx 条回复”
11+
const pagingCells = $notCommentCells.slice(1).addClass('v2p-paging')
12+
1113
const pageBtns = pagingCells.find('.super.button')
1214
pageBtns.eq(0).addClass('v2p-prev-btn')
1315
pageBtns.eq(1).addClass('v2p-next-btn')

src/services.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,3 +218,9 @@ export async function setV2P_Settings(storageSettings: StorageSettings) {
218218

219219
await setStorage(StorageKey.SyncInfo, syncInfo)
220220
}
221+
222+
export async function fetchTopicPage(path: string, page: string) {
223+
const res = await fetch(`${V2EX.Origin}${path}?p=${page}`)
224+
const htmlText = await res.text()
225+
return htmlText
226+
}

0 commit comments

Comments
 (0)