js/mod/recent_posts.js: remember state on reload
This commit is contained in:
parent
b8af556b2a
commit
aa4d943ff4
|
@ -24,13 +24,14 @@
|
||||||
const kLocationPath = location.href.slice(location.origin.length)
|
const kLocationPath = location.href.slice(location.origin.length)
|
||||||
if (/^\/mod\.php\?\/recent\/\d+$/.test(kLocationPath)) {
|
if (/^\/mod\.php\?\/recent\/\d+$/.test(kLocationPath)) {
|
||||||
const loadIntFromStorage = key => {
|
const loadIntFromStorage = key => {
|
||||||
const value = localStorage.getItem(`jon-liveposts-enabled::${key}`)
|
const value = localStorage.getItem(`jon-liveposts::${key}`)
|
||||||
return value != null ? parseInt(value) : null
|
return value != null ? parseInt(value) : null
|
||||||
}
|
}
|
||||||
|
|
||||||
const kPullXPosts = loadIntFromStorage("pull-x-posts") ?? 6
|
const kPullXPosts = loadIntFromStorage("pull-x-posts") ?? 6
|
||||||
const kPullEveryX = loadIntFromStorage("pull-every-x") ?? 8000
|
const kPullEveryX = loadIntFromStorage("pull-every-x") ?? 8000
|
||||||
const kRecentXPosts = parseInt(kLocationPath.slice(17).split(/[^\d]/)[0])
|
const kRecentXPosts = parseInt(kLocationPath.slice(17).split(/[^\d]/)[0])
|
||||||
|
const kWasEnabled = loadIntFromStorage("enabled") == 1
|
||||||
|
|
||||||
let liveUpdateEnabled = false
|
let liveUpdateEnabled = false
|
||||||
let liveUpdateAbortController = null
|
let liveUpdateAbortController = null
|
||||||
|
@ -103,6 +104,7 @@
|
||||||
const checkbox = document.createElement("input")
|
const checkbox = document.createElement("input")
|
||||||
checkbox.type = "checkbox"
|
checkbox.type = "checkbox"
|
||||||
checkbox.id = id
|
checkbox.id = id
|
||||||
|
checkbox.checked = kWasEnabled
|
||||||
label.innerText = "live update: "
|
label.innerText = "live update: "
|
||||||
label.for = id
|
label.for = id
|
||||||
div.style.display = "inline"
|
div.style.display = "inline"
|
||||||
|
@ -116,20 +118,27 @@
|
||||||
const pageNums = Array.prototype.find.apply(document.body.children, [ el => el.nodeName == "P" ])
|
const pageNums = Array.prototype.find.apply(document.body.children, [ el => el.nodeName == "P" ])
|
||||||
const form = createCheckbox()
|
const form = createCheckbox()
|
||||||
|
|
||||||
form.checkbox.addEventListener("click", () => {
|
const liveUpdateToggle = enabled => {
|
||||||
setTimeout(() => {
|
if (enabled) {
|
||||||
if (form.checkbox.checked) {
|
liveUpdateEnabled = true
|
||||||
liveUpdateEnabled = true
|
liveUpdateAbortController = new AbortController()
|
||||||
liveUpdateAbortController = new AbortController()
|
liveUpdateIntervalId = setInterval(() => updateRecentPosts(), kPullEveryX)
|
||||||
liveUpdateIntervalId = setInterval(() => updateRecentPosts(), kPullEveryX)
|
} else {
|
||||||
} else {
|
liveUpdateEnabled = false
|
||||||
liveUpdateEnabled = false
|
clearInterval(liveUpdateIntervalId)
|
||||||
clearInterval(liveUpdateIntervalId)
|
liveUpdateAbortController.abort()
|
||||||
liveUpdateAbortController.abort()
|
liveUpdateIntervalId = null
|
||||||
liveUpdateIntervalId = null
|
liveUpdateAbortController = null
|
||||||
liveUpdateAbortController = null
|
}
|
||||||
}
|
|
||||||
}, 700)
|
if (form.checkbox.checked != enabled) {
|
||||||
|
form.checkbox.checked = enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
form.checkbox.addEventListener("change", () => {
|
||||||
|
localStorage.setItem("jon-liveposts::enabled", form.checkbox.checked ? "1" : "0")
|
||||||
|
liveUpdateToggle(form.checkbox.checked)
|
||||||
})
|
})
|
||||||
|
|
||||||
document.body.addEventListener("mousemove", () => {
|
document.body.addEventListener("mousemove", () => {
|
||||||
|
@ -141,6 +150,10 @@
|
||||||
|
|
||||||
pageNums.append("|")
|
pageNums.append("|")
|
||||||
pageNums.append(form.div)
|
pageNums.append(form.div)
|
||||||
|
|
||||||
|
if (kWasEnabled) {
|
||||||
|
setTimeout(() => liveUpdateToggle(true), 1)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue