js/mod/recent_posts.js: polyfill AbortSignal.any, add load wrapper, silence AbortSignal DOMException warn
This commit is contained in:
parent
960919c2dd
commit
aea9793160
|
@ -2,6 +2,27 @@
|
|||
* Implements live-reloading on the recent posts mod page.
|
||||
*/
|
||||
(() => {
|
||||
if (AbortSignal.any === undefined) {
|
||||
AbortSignal.any = function (signals) {
|
||||
const controller = new AbortController()
|
||||
const callbacks = []
|
||||
|
||||
const abortFn = () => {
|
||||
for (const signal of signals) {
|
||||
signal.removeEventListener("abort", abortFn)
|
||||
}
|
||||
controller.abort()
|
||||
}
|
||||
|
||||
for (const signal of signals) {
|
||||
signal.addEventListener("abort", abortFn)
|
||||
}
|
||||
|
||||
return controller.signal
|
||||
}
|
||||
}
|
||||
|
||||
const scriptFn = () => {
|
||||
const kLocationPath = location.href.slice(location.origin.length)
|
||||
if (/^\/mod\.php\?\/recent\/\d+$/.test(kLocationPath)) {
|
||||
const loadIntFromStorage = key => {
|
||||
|
@ -38,11 +59,13 @@
|
|||
}
|
||||
|
||||
const getPostTimestamp = post => new Date(post.lastElementChild.querySelector(".intro time").dateTime)
|
||||
const updateRecentPosts = async () => {
|
||||
const updateRecentPosts = () => {
|
||||
if (liveUpdateEnabled) {
|
||||
fetchLatest()
|
||||
.then(latestPosts => {
|
||||
const lastPost = document.body.querySelector(".post-wrapper")
|
||||
const lastPostTs = getPostTimestamp(lastPost).getTime()
|
||||
const posts = (await fetchLatest()).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) {
|
||||
const totalPosts = Array.from(document.body.querySelectorAll(".post-wrapper"))
|
||||
for (const post of posts) {
|
||||
|
@ -58,6 +81,13 @@
|
|||
makeIcon("reply")
|
||||
liveUpdateFaviconChanged = true
|
||||
}
|
||||
})
|
||||
.catch(error => {
|
||||
// XXX: Why the hell does gecko have a space at the end??
|
||||
if (!((error instanceof DOMException) && [ "The user aborted a request", "The operation was aborted." ].some(msg => error.message.slice(0, 26) === msg))) {
|
||||
throw error
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -114,4 +144,12 @@
|
|||
pageNums.append("|")
|
||||
pageNums.append(form.div)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if (document.readyState != "complete") {
|
||||
window.addEventListener("load", scriptFn, { "once": true })
|
||||
} else {
|
||||
setTimeout(scriptFn, 1)
|
||||
}
|
||||
})()
|
||||
|
|
Loading…
Reference in New Issue