parse authors

This commit is contained in:
Hongbo Wu 2022-11-29 12:26:37 +08:00
parent c993fa11be
commit 8ee19a98fa

View file

@ -10,7 +10,7 @@ export class StackOverflowHandler extends ContentHandler {
const votes = element.querySelector(`div[itemprop='upvoteCount']`)
if (votes) {
const newVotes = dom.createElement('div')
newVotes.innerHTML = `<h3>${title}: ${votes.innerHTML}votes</h3>`
newVotes.innerHTML = `<h3>${title}: ${votes.innerHTML}vote(s)</h3>`
element.prepend(newVotes)
}
}
@ -21,26 +21,24 @@ export class StackOverflowHandler extends ContentHandler {
if (commentsDiv) {
const comments = commentsDiv.querySelectorAll(`.comment`)
if (comments.length > 0) {
const count = element.querySelector(
`span[itemprop='commentCount']`
)?.textContent
// const count = element.querySelector(
// `span[itemprop='commentCount']`
// )?.textContent
const newComments = dom.createElement('div')
newComments.innerHTML = `<h3>${
count ? count + ' Comments' : 'Comment'
}</h3>`
newComments.innerHTML = `<h4>Comments</h4>`
// newComments.innerHTML = `<h4>${
// count ? count + ' Comments' : 'Comment'
// }</h4>`
comments.forEach((comment) => {
const author = comment.querySelector(`.comment-user`)
const text = comment.querySelector(`.comment-copy`)?.textContent
const authorHref = author?.getAttribute('href')
const date = comment.querySelector(`.relativetime-clean`)?.textContent
const link = comment
.querySelector(`.comment-link`)
?.getAttribute('href')
if (author && text && authorHref && date && link) {
if (author && text && authorHref && date) {
const newComment = dom.createElement('p')
newComment.innerHTML = `<a href="${authorHref}">${author.innerHTML}</a>: ${text} - <a href="${link}">${date}</a>`
newComment.innerHTML = `<a href="${authorHref}">${author.innerHTML}</a>: ${text} - ${date}`
newComments.appendChild(newComment)
}
})
@ -52,6 +50,26 @@ export class StackOverflowHandler extends ContentHandler {
element.querySelector(`span[itemprop='commentCount']`)?.remove()
}
parseUser(element: Element, dom: Document) {
const users = element.querySelectorAll(`.post-signature`)
users.forEach((user) => {
const name = user.querySelector(`.user-details a`)?.textContent
const link = user.querySelector(`.user-details a`)?.getAttribute('href')
const reputation = user.querySelector(`.reputation-score`)?.textContent
const badges = Array.from(user.querySelectorAll(`span[title*='badges']`))
.map((badge) => badge.getAttribute('title'))
.join(', ')
const date = user.querySelector(`.user-action-time`)?.textContent
if (name && link && reputation && date) {
const newUser = dom.createElement('p')
newUser.innerHTML = `By <a href="${link}">${name}</a> - ${reputation} reputation - ${
badges || 'no badge'
} - ${date}`
element.replaceChild(newUser, user)
}
})
}
shouldPreParse(url: string, dom: Document): boolean {
return new URL(url).hostname.endsWith('stackoverflow.com')
}
@ -63,17 +81,19 @@ export class StackOverflowHandler extends ContentHandler {
if (question) {
this.parseVotes(question, dom, 'Question')
this.parseComments(question, dom)
this.parseUser(question, dom)
}
const answersDiv = mainEntity.querySelector('#answers')
if (answersDiv) {
const count = mainEntity.querySelector(
`span[itemprop='answerCount']`
)?.textContent
// const count = mainEntity.querySelector(
// `span[itemprop='answerCount']`
// )?.textContent
const newAnswers = dom.createElement('div')
newAnswers.innerHTML = `<h3>${
count ? count + ' Answers' : 'Answer'
}</h3>`
newAnswers.innerHTML = `<h2>Answers</h2>`
// newAnswers.innerHTML = `<h2>${
// count ? count + ' Answers' : 'Answer'
// }</h2>`
const answers = answersDiv.querySelectorAll(`.answer`)
answers.forEach((answer) => {
@ -82,6 +102,7 @@ export class StackOverflowHandler extends ContentHandler {
: 'Answer'
this.parseVotes(answer, dom, title)
this.parseComments(answer, dom)
this.parseUser(answer, dom)
newAnswers.appendChild(answer)
})
answersDiv.replaceChildren(newAnswers)