Compare commits

..

11 Commits

2 changed files with 32 additions and 15 deletions

View File

@ -389,6 +389,7 @@ globalThis.LCNSetting = class LCNSetting {
this._id = null;
this._eventId = null;
this._label = null;
this._hidden = false;
this._value = null;
this._valueDefault = null;
@ -405,6 +406,8 @@ 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) {
@ -421,7 +424,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 {
@ -431,7 +437,7 @@ globalThis.LCNToggleSetting = class LCNToggleSetting extends LCNSetting {
const div = document.createElement("div")
const chk = document.createElement("input")
const lbl = document.createElement("label")
const id = `${this._id}_input`
const id = `lcnts::${this.id}`
lbl.htmlFor = id
lbl.innerText = this.getLabel()
chk.id = id
@ -486,7 +492,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

@ -5,12 +5,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));
.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))*/;
@ -35,8 +40,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 => {
@ -58,7 +63,7 @@ $().ready(() => {
}
const findMissingReplies = (thread_op, thread_dom, thread_latest) => {
const lastPostTs = (cont(thread_dom.at(-1), x => x.getInfo()) || thread_op).getCreatedAt().getTime()
const lastPostTs = (cont(thread_dom.at(-1), x => x.getInfo()) || thread_op.getInfo()).getCreatedAt().getTime()
const missing = []
for (const pc of thread_latest.reverse()) {
@ -131,7 +136,7 @@ $().ready(() => {
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
@ -152,23 +157,29 @@ $().ready(() => {
}
const refreshFn = () => {
if (secondsCounter >= 0) {
secondsCounter = 0
onTickFn()
}
}
$(document).on("ajax_after_post", (_, xhr_body) => {
if (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()
}
})