Compare commits

...

3 Commits

2 changed files with 34 additions and 13 deletions

View File

@ -356,6 +356,7 @@ globalThis.LCNSetting = class LCNSetting {
#id = null;
#eventId = null;
#label = null;
#hidden = false;
#value = null;
#valueDefault = null;
@ -375,6 +376,9 @@ globalThis.LCNSetting = class LCNSetting {
}
}
"isHidden" () { return this.#hidden; }
"setHidden" (v) { this.#hidden = v; return this; }
"getValue" () { return this.#value ?? (this.#value = this.#getValue()); }
"setValue" (v) {
if (this.#value !== v) {
@ -391,7 +395,10 @@ globalThis.LCNSetting = class LCNSetting {
"setDefaultValue" (vd) { this.#valueDefault = vd; return this; }
"onChange" (fn) { $(document).on(`${this.#eventId}::change`, (_,v,i) => fn(v, i)); }
__setIdPrefix (prefix) { this.#id = `${prefix}_${this.#id}`; }
__setIdPrefix (prefix) {
this.#id = `${prefix}_${this.#id}`
this.#eventId = `lcnsetting::${this.#id}`
}
}
globalThis.LCNToggleSetting = class LCNToggleSetting extends LCNSetting {
@ -401,7 +408,10 @@ globalThis.LCNToggleSetting = class LCNToggleSetting extends LCNSetting {
const div = document.createElement("div")
const chk = document.createElement("input")
const txt = document.createElement("label")
const id = `lcnts::${this.id}`
txt.id = id
txt.innerText = this.getLabel()
chk.id = id
chk.type = "checkbox"
chk.checked = this.getValue()
chk.addEventListener("click", e => {
@ -459,7 +469,7 @@ globalThis.LCNSettingsSubcategory = class LCNSettingsSubcategory {
"addSetting" (setting) {
assert.ok(setting instanceof LCNSetting)
setting.__setIdPrefix(`lcnsetting_${this.#tab_id}_${this.#id}`)
if (setting.__builtinDOMConstructor != null) {
if (!setting.isHidden() && setting.__builtinDOMConstructor != null) {
const div = setting.__builtinDOMConstructor()
div.classList.add("lcn-setting-entry")
this.#fieldset.appendChild(div)

View File

@ -6,12 +6,17 @@
$().ready(() => {
const kIsEnabled = LCNToggleSetting.build("enabled")
const kUpdateOnReplyEnabled = LCNToggleSetting.build("updateOnReplyEnabled")
//const kIsBellEnabled = LCNToggleSetting.build("bellEnabled")
void LCNSettingsSubcategory.for("general", "threadUpdater")
.setLabel("Thread Updater")
.addSetting(kIsEnabled
.setLabel(_("Fetch new replies in the background"))
.setDefaultValue(true))
.addSetting(kUpdateOnReplyEnabled
.setLabel(_("Update thread after sending a reply"))
.setHidden(true)
.setDefaultValue(true))
/*.addSetting(kIsBellEnabled
.setLabel(_("Play an audible chime when new replies are found"))
.setDefaultValue(false))*/;
@ -36,8 +41,8 @@ $().ready(() => {
const updateSecondsByTSLP = post_info => {
secondsCounter = Math.floor(((Date.now() - post_info.getCreatedAt().getTime()) / 120000))
secondsCounter = secondsCounter > 1000 ? 1000 : secondsCounter
secondsCounter = secondsCounter < 11 ? 11 : secondsCounter
secondsCounter = Math.min(1000, secondsCounter)
secondsCounter = Math.max(11, secondsCounter)
}
const updateStatsFn = async thread => {
@ -59,7 +64,7 @@ $().ready(() => {
}
const findMissingReplies = (thread_op, thread_dom, thread_latest) => {
const lastPostTs = (thread_dom.at(-1)?.getInfo() ?? thread_op).getCreatedAt().getTime()
const lastPostTs = (thread_dom.at(-1)?.getInfo() ?? thread_op.getInfo()).getCreatedAt().getTime()
const missing = []
for (const pc of thread_latest.reverse()) {
@ -131,7 +136,7 @@ $().ready(() => {
const thread = LCNThread.first()
try {
await updateStatsFn(thread)
if (threadState == null && threadStats.last_modified > (thread.getReplies().at(-1).getInfo().getCreatedAt().getTime() / 1000)) {
if (threadState == null && threadStats.last_modified > (thread.getPosts().at(-1).getInfo().getCreatedAt().getTime() / 1000)) {
updateThreadFn(thread, await fetchThreadFn())
}
@ -140,7 +145,7 @@ $().ready(() => {
statReplies.innerText = thread.getReplies().length
statFiles.innerText = threadEl.querySelectorAll(".files .file").length - threadEl.querySelectorAll(".files .file .post-image.deleted").length
statPage.innerText = threadStats.page + 1
updateSecondsByTSLP(thread.getReplies().at(-1).getInfo())
updateSecondsByTSLP(thread.getPosts().at(-1).getInfo())
} catch (error) {
console.error("threadAutoUpdater: Failed while processing update. Probably a network error", error)
secondsCounter = 60
@ -151,23 +156,29 @@ $().ready(() => {
}
}
const refreshFn = () => {
if (secondsCounter >= 0) {
secondsCounter = 0
onTickFn()
}
}
$(document).on("ajax_after_post", (_, xhr_body) => {
if (kIsEnabled.getValue() && xhr_body != null) {
if (kUpdateOnReplyEnabled.getValue() && xhr_body != null) {
if (!xhr_body.mod) {
const thread = LCNThread.first()
const dom = parser.parseFromString(xhr_body.thread, "text/html")
updateThreadFn(thread, dom)
updateSecondsByTSLP(thread.getReplies().at(-1).getInfo())
updateSecondsByTSLP(thread.getPosts().at(-1).getInfo())
} else {
$(document).trigger("thread_manual_refresh")
refreshFn()
}
}
})
$(document).on("thread_manual_refresh", () => {
if (kIsEnabled.getValue() && secondsCounter >= 0) {
secondsCounter = 0
onTickFn()
if (kIsEnabled.getValue()) {
refreshFn()
}
})