2024-01-30 18:47:20 +00:00
|
|
|
/**
|
|
|
|
* Implements live-reloading on the recent posts mod page.
|
|
|
|
*/
|
|
|
|
(() => {
|
2024-01-31 02:32:42 +00:00
|
|
|
if (AbortSignal.any === undefined) {
|
|
|
|
AbortSignal.any = function (signals) {
|
|
|
|
const controller = new AbortController()
|
|
|
|
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
|
2024-01-30 18:47:20 +00:00
|
|
|
}
|
2024-01-31 02:32:42 +00:00
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
const scriptFn = () => {
|
|
|
|
const kLocationPath = location.href.slice(location.origin.length)
|
|
|
|
if (/^\/mod\.php\?\/recent\/\d+$/.test(kLocationPath)) {
|
|
|
|
const loadIntFromStorage = key => {
|
2024-01-31 03:22:55 +00:00
|
|
|
const value = localStorage.getItem(`jon-liveposts::${key}`)
|
2024-01-31 02:32:42 +00:00
|
|
|
return value != null ? parseInt(value) : null
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
const kPullXPosts = loadIntFromStorage("pull-x-posts") ?? 6
|
|
|
|
const kPullEveryX = loadIntFromStorage("pull-every-x") ?? 8000
|
|
|
|
const kRecentXPosts = parseInt(kLocationPath.slice(17).split(/[^\d]/)[0])
|
2024-01-31 03:22:55 +00:00
|
|
|
const kWasEnabled = loadIntFromStorage("enabled") == 1
|
2024-02-02 18:08:49 +00:00
|
|
|
const kBellWasEnabled = loadIntFromStorage("bell-enabled") == 1
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
let liveUpdateEnabled = false
|
2024-02-02 18:08:49 +00:00
|
|
|
let liveUpdateBellEnabled = kBellWasEnabled
|
2024-01-31 02:32:42 +00:00
|
|
|
let liveUpdateAbortController = null
|
|
|
|
let liveUpdateIntervalId = null
|
|
|
|
let liveUpdateFaviconChanged = false
|
|
|
|
|
|
|
|
const parser = new DOMParser()
|
|
|
|
const fetchLatest = async () => {
|
|
|
|
const res = await fetch(`/mod.php?/recent/${kPullXPosts}`, {
|
|
|
|
"signal": AbortSignal.any([ liveUpdateAbortController.signal, AbortSignal.timeout(kPullEveryX - 500) ])
|
|
|
|
})
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
if (res.ok) {
|
|
|
|
const dom = parser.parseFromString(await res.text(), "text/html")
|
|
|
|
const posts = Array.from(dom.querySelectorAll("body > .post-wrapper[data-board]"))
|
2024-02-20 18:28:33 +00:00
|
|
|
const eita = Array.prototype.find.apply(el.children, [ el => el.classList.contains("eita-link") ])
|
2024-01-31 02:32:42 +00:00
|
|
|
return posts.map(el => ({
|
|
|
|
"element": el,
|
2024-02-20 18:28:33 +00:00
|
|
|
"post": el.querySelector(".thread, .post"),
|
|
|
|
"href": eita.href,
|
|
|
|
"id": eita.id
|
2024-01-31 02:32:42 +00:00
|
|
|
}))
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
const getPostTimestamp = post => new Date(post.lastElementChild.querySelector(".intro time").dateTime)
|
|
|
|
const updateRecentPosts = () => {
|
|
|
|
if (liveUpdateEnabled) {
|
|
|
|
fetchLatest()
|
|
|
|
.then(latestPosts => {
|
2024-01-31 03:58:57 +00:00
|
|
|
const lastPost = Array.prototype.find.apply(document.body.children, [ el => el.classList.contains("post-wrapper") ])
|
2024-01-31 02:32:42 +00:00
|
|
|
const lastPostTs = getPostTimestamp(lastPost).getTime()
|
|
|
|
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) {
|
2024-01-31 03:58:57 +00:00
|
|
|
document.body.insertBefore(post.element, lastPost)
|
2024-01-31 02:32:42 +00:00
|
|
|
totalPosts.unshift(post.element)
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
while (totalPosts.length > kRecentXPosts) {
|
|
|
|
document.body.removeChild(totalPosts.pop())
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 03:58:57 +00:00
|
|
|
// XXX: Fire ::new_post for chanx listeners
|
|
|
|
for (const post of posts.slice(0, kRecentXPosts)) {
|
2024-02-20 18:28:33 +00:00
|
|
|
$(document).trigger("new_post", [ post.post ])
|
2024-01-31 03:58:57 +00:00
|
|
|
}
|
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
updatePageNext()
|
|
|
|
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
|
|
|
|
}
|
|
|
|
})
|
2024-01-30 18:47:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
const pageNext = Array.prototype.find.apply(document.body.children, [ el => el.nodeName == "A" && el.href.startsWith(`${location.origin}/mod.php?/recent/${kRecentXPosts}&last=`) ])
|
|
|
|
const updatePageNext = () => {
|
|
|
|
const posts = document.body.querySelectorAll(".post-wrapper")
|
|
|
|
const oldestPost = posts[posts.length-1]
|
|
|
|
pageNext.href = `/mod.php?/recent/${kRecentXPosts}&last=${getPostTimestamp(oldestPost).getTime() / 1000}`
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-02-02 18:08:49 +00:00
|
|
|
const createCheckbox = (id, text, initialState) => {
|
2024-01-31 02:32:42 +00:00
|
|
|
const div = document.createElement("div")
|
|
|
|
const label = document.createElement("label")
|
|
|
|
const checkbox = document.createElement("input")
|
|
|
|
checkbox.type = "checkbox"
|
|
|
|
checkbox.id = id
|
2024-02-02 18:08:49 +00:00
|
|
|
checkbox.checked = initialState
|
|
|
|
label.innerText = text
|
2024-01-31 02:32:42 +00:00
|
|
|
label.for = id
|
|
|
|
div.style.display = "inline"
|
|
|
|
div.append(" ")
|
|
|
|
div.appendChild(label)
|
|
|
|
div.appendChild(checkbox)
|
|
|
|
div.append(" ")
|
|
|
|
return { checkbox, label, div }
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
2024-01-31 03:22:55 +00:00
|
|
|
const liveUpdateToggle = enabled => {
|
|
|
|
if (enabled) {
|
|
|
|
liveUpdateEnabled = true
|
|
|
|
liveUpdateAbortController = new AbortController()
|
|
|
|
liveUpdateIntervalId = setInterval(() => updateRecentPosts(), kPullEveryX)
|
|
|
|
} else {
|
|
|
|
liveUpdateEnabled = false
|
|
|
|
clearInterval(liveUpdateIntervalId)
|
|
|
|
liveUpdateAbortController.abort()
|
|
|
|
liveUpdateIntervalId = null
|
|
|
|
liveUpdateAbortController = null
|
|
|
|
}
|
|
|
|
|
2024-02-02 18:08:49 +00:00
|
|
|
if (kLiveForm.checkbox.checked != enabled) {
|
|
|
|
kLiveForm.checkbox.checked = enabled
|
2024-01-31 03:22:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-02 18:08:49 +00:00
|
|
|
const pageNums = Array.prototype.find.apply(document.body.children, [ el => el.nodeName == "P" ])
|
|
|
|
const kLiveForm = createCheckbox("jon-liveposts-enabled", "live: ", kWasEnabled)
|
|
|
|
const kBellForm = createCheckbox("jon-liveposts-bell-enabled", "beep: ", kBellWasEnabled)
|
|
|
|
const kBell = new Audio("/static/jannybell.mp3")
|
|
|
|
|
|
|
|
kLiveForm.checkbox.addEventListener("change", () => {
|
|
|
|
localStorage.setItem("jon-liveposts::enabled", kLiveForm.checkbox.checked ? "1" : "0")
|
|
|
|
liveUpdateToggle(kLiveForm.checkbox.checked)
|
|
|
|
})
|
|
|
|
|
|
|
|
kBellForm.checkbox.addEventListener("change", () => {
|
|
|
|
localStorage.setItem("jon-liveposts::bell-enabled", kBellForm.checkbox.checked ? "1" : "0")
|
|
|
|
liveUpdateBellEnabled = kBellForm.checkbox.checked
|
2024-01-31 02:32:42 +00:00
|
|
|
})
|
|
|
|
|
2024-02-02 18:08:49 +00:00
|
|
|
$(document).on("new_post", () => {
|
|
|
|
if (liveUpdateBellEnabled && kBell.paused) {
|
|
|
|
// XXX: Site requires autoplay media permission to do this
|
|
|
|
kBell.play().catch(console.error)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for (const form of [ kLiveForm, kBellForm ]) {
|
|
|
|
pageNums.append("|")
|
|
|
|
pageNums.append(form.div)
|
|
|
|
}
|
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
document.body.addEventListener("mousemove", () => {
|
|
|
|
if (liveUpdateFaviconChanged) {
|
|
|
|
liveUpdateFaviconChanged = false
|
|
|
|
makeIcon(false)
|
2024-01-30 18:47:20 +00:00
|
|
|
}
|
2024-01-31 02:32:42 +00:00
|
|
|
})
|
|
|
|
|
2024-01-31 03:22:55 +00:00
|
|
|
if (kWasEnabled) {
|
|
|
|
setTimeout(() => liveUpdateToggle(true), 1)
|
|
|
|
}
|
2024-01-31 02:32:42 +00:00
|
|
|
}
|
|
|
|
}
|
2024-01-30 18:47:20 +00:00
|
|
|
|
|
|
|
|
2024-01-31 02:32:42 +00:00
|
|
|
if (document.readyState != "complete") {
|
|
|
|
window.addEventListener("load", scriptFn, { "once": true })
|
|
|
|
} else {
|
|
|
|
setTimeout(scriptFn, 1)
|
2024-01-30 18:47:20 +00:00
|
|
|
}
|
|
|
|
})()
|