js/mod/recent_posts.js: fire ::new_post for chanx listeners, use insertBefore instead of prepend
This commit is contained in:
parent
aa4d943ff4
commit
862536f7b5
|
@ -38,6 +38,10 @@
|
||||||
let liveUpdateIntervalId = null
|
let liveUpdateIntervalId = null
|
||||||
let liveUpdateFaviconChanged = false
|
let liveUpdateFaviconChanged = false
|
||||||
|
|
||||||
|
const getChanComment = el => Array.prototype.find.apply(el.children, [
|
||||||
|
el => el.classList.contains("thread") || el.classList.contains("postcontainer")
|
||||||
|
])
|
||||||
|
|
||||||
const parser = new DOMParser()
|
const parser = new DOMParser()
|
||||||
const fetchLatest = async () => {
|
const fetchLatest = async () => {
|
||||||
const res = await fetch(`/mod.php?/recent/${kPullXPosts}`, {
|
const res = await fetch(`/mod.php?/recent/${kPullXPosts}`, {
|
||||||
|
@ -50,7 +54,7 @@
|
||||||
return posts.map(el => ({
|
return posts.map(el => ({
|
||||||
"element": el,
|
"element": el,
|
||||||
"href": Array.prototype.find.apply(el.children, [ el => el.classList.contains("eita-link") ]).href,
|
"href": Array.prototype.find.apply(el.children, [ el => el.classList.contains("eita-link") ]).href,
|
||||||
"id": Array.prototype.find.apply(el.children, [ el => el.classList.contains("thread") || el.classList.contains("postcontainer") ]).id
|
"id": getChanComment(el).id
|
||||||
}))
|
}))
|
||||||
} else {
|
} else {
|
||||||
return []
|
return []
|
||||||
|
@ -62,13 +66,13 @@
|
||||||
if (liveUpdateEnabled) {
|
if (liveUpdateEnabled) {
|
||||||
fetchLatest()
|
fetchLatest()
|
||||||
.then(latestPosts => {
|
.then(latestPosts => {
|
||||||
const lastPost = document.body.querySelector(".post-wrapper")
|
const lastPost = Array.prototype.find.apply(document.body.children, [ el => el.classList.contains("post-wrapper") ])
|
||||||
const lastPostTs = getPostTimestamp(lastPost).getTime()
|
const lastPostTs = getPostTimestamp(lastPost).getTime()
|
||||||
const posts = latestPosts.filter(post => document.getElementById(post.id) == null && getPostTimestamp(post.element).getTime() > lastPostTs)
|
const posts = latestPosts.filter(post => document.getElementById(post.id) == null && getPostTimestamp(post.element).getTime() > lastPostTs)
|
||||||
if (posts.length > 0) {
|
if (posts.length > 0) {
|
||||||
const totalPosts = Array.from(document.body.querySelectorAll(".post-wrapper"))
|
const totalPosts = Array.from(document.body.querySelectorAll(".post-wrapper"))
|
||||||
for (const post of posts) {
|
for (const post of posts) {
|
||||||
lastPost.prepend(post.element)
|
document.body.insertBefore(post.element, lastPost)
|
||||||
totalPosts.unshift(post.element)
|
totalPosts.unshift(post.element)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -76,6 +80,11 @@
|
||||||
document.body.removeChild(totalPosts.pop())
|
document.body.removeChild(totalPosts.pop())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// XXX: Fire ::new_post for chanx listeners
|
||||||
|
for (const post of posts.slice(0, kRecentXPosts)) {
|
||||||
|
$(document).trigger("new_post", [ getChanComment(post.element) ])
|
||||||
|
}
|
||||||
|
|
||||||
updatePageNext()
|
updatePageNext()
|
||||||
makeIcon("reply")
|
makeIcon("reply")
|
||||||
liveUpdateFaviconChanged = true
|
liveUpdateFaviconChanged = true
|
||||||
|
|
Loading…
Reference in New Issue