Compare commits

...

7 Commits

Author SHA1 Message Date
towards-a-new-leftypol 96eb798684 get some of classes.js building 2024-02-27 05:20:43 +00:00
towards-a-new-leftypol e122d86fb7 WIP: slowly but surely 2024-02-27 02:58:55 +00:00
Jon ebb7cb8cfb
thread_updater.js: cleanup, use thread_manual_refresh 2024-02-26 21:49:46 +00:00
towards-a-new-leftypol ba120d32f7 Thread updator - handle case where you're a mod
- mod triggers refresh
- not a mod uses the response
2024-02-26 21:32:04 +00:00
Jon 07db8e2db4 thread_autoupdater.js: update tslp on ajax post 2024-02-26 21:32:04 +00:00
Jon c5cf671929 thread_autoupdater.js: cleanup 2024-02-26 21:32:04 +00:00
Jon 733aad3bf2 js/lcn/utils.js: use captureStackTrace if available 2024-02-26 21:32:04 +00:00
4 changed files with 495 additions and 483 deletions

View File

@ -4,7 +4,6 @@
*/ */
globalThis.LCNSite = class LCNSite { globalThis.LCNSite = class LCNSite {
static INSTANCE = null;
static "createAbortable" () { static "createAbortable" () {
const obj = { "abort": null, "controller": null, "signal": null } const obj = { "abort": null, "controller": null, "signal": null }
@ -32,54 +31,62 @@ globalThis.LCNSite = class LCNSite {
} }
return null return null
};
static "constructor" () {
this._isModerator = document.body.classList.contains("is-moderator");
this._isThreadPage = document.body.classList.contains("active-thread");
this._isBoardPage = document.body.classList.contains("active-board");
this._isCatalogPage = document.body.classList.contains("active-catalog");
this._isModPage = location.pathname == "/mod.php";
this._isModRecentsPage = this._isModPage && (location.search == "?/recent" || location.search.startsWith("?/recent/"));
this._isModReportsPage = this._isModPage && (location.search == "?/reports" || location.search.startsWith("?/reports/"));
this._isModLogPage = this._isModPage && (location.search == "?/log" || location.search.startsWith("?/log/"));
this._unseen = 0;
this._pageTitle = document.title;
this._doTitleUpdate = () => {
document.title = (this._unseen > 0 ? `(${this._unseen}) ` : "") + this._pageTitle;
};
this._favicon = document.querySelector("head > link[rel=\"shortcut icon\"]");
this._generatedStyle = null;
} }
#isModerator = document.body.classList.contains("is-moderator"); "isModerator" () { return this._isModerator; }
#isThreadPage = document.body.classList.contains("active-thread"); "isThreadPage" () { return this._isThreadPage; }
#isBoardPage = document.body.classList.contains("active-board"); "isBoardPage" () { return this._isBoardPage; }
#isCatalogPage = document.body.classList.contains("active-catalog"); "isCatalogPage" () { return this._isCatalogPage; }
#isModPage = location.pathname == "/mod.php"; "isModPage" () { return this._isModPage; }
#isModRecentsPage = this.#isModPage && (location.search == "?/recent" || location.search.startsWith("?/recent/")); "isModRecentsPage" () { return this._isModRecentsPage; }
#isModReportsPage = this.#isModPage && (location.search == "?/reports" || location.search.startsWith("?/reports/")); "isModReportsPage" () { return this._isModReportsPage; }
#isModLogPage = this.#isModPage && (location.search == "?/log" || location.search.startsWith("?/log/")); "isModLogPage" () { return this._isModLogPage; }
"isModerator" () { return this.#isModerator; } "getUnseen" () { return this._unseen; }
"isThreadPage" () { return this.#isThreadPage; } "clearUnseen" () { if (this._unseen != 0) { this.setUnseen(0); } }
"isBoardPage" () { return this.#isBoardPage; }
"isCatalogPage" () { return this.#isCatalogPage; }
"isModPage" () { return this.#isModPage; }
"isModRecentsPage" () { return this.#isModRecentsPage; }
"isModReportsPage" () { return this.#isModReportsPage; }
"isModLogPage" () { return this.#isModLogPage; }
#unseen = 0;
"getUnseen" () { return this.#unseen; }
"clearUnseen" () { if (this.#unseen != 0) { this.setUnseen(0); } }
"setUnseen" (int) { "setUnseen" (int) {
const bool = !!int const bool = !!int
if (bool != !!this.#unseen) { if (bool != !!this._unseen) {
this.setFaviconType(bool ? "reply" : null) this.setFaviconType(bool ? "reply" : null)
} }
this.#unseen = int this._unseen = int
this.#doTitleUpdate() this._doTitleUpdate()
} }
#pageTitle = document.title; "getTitle" () { return this._pageTitle; }
"getTitle" () { return this.#pageTitle; } "setTitle" (title) { this._pageTitle = title; this._doTitleUpdate(); }
"setTitle" (title) { this.#pageTitle = title; this.#doTitleUpdate(); }
#doTitleUpdate () { document.title = (this.#unseen > 0 ? `(${this.#unseen}) ` : "") + this.#pageTitle; }
#favicon = document.querySelector("head > link[rel=\"shortcut icon\"]");
"setFaviconType" (type=null) { "setFaviconType" (type=null) {
if (this.#favicon == null) { if (this._favicon == null) {
this.#favicon = document.createElement("link") this._favicon = document.createElement("link")
this.#favicon.rel = "shortcut icon" this._favicon.rel = "shortcut icon"
document.head.appendChild(this.#favicon) document.head.appendChild(this._favicon)
} }
this.#favicon.href = `/favicon${type ? "-" + type : ""}.ico` this._favicon.href = `/favicon${type ? "-" + type : ""}.ico`
} }
"getFloaterLContainer" () { return document.getElementById("bar-bottom-l"); } "getFloaterLContainer" () { return document.getElementById("bar-bottom-l"); }
@ -87,38 +94,39 @@ globalThis.LCNSite = class LCNSite {
"getThreadStatsLContainer" () { return document.getElementById("lcn-threadstats-l"); } "getThreadStatsLContainer" () { return document.getElementById("lcn-threadstats-l"); }
"getThreadStatsRContainer" () { return document.getElementById("lcn-threadstats-r"); } "getThreadStatsRContainer" () { return document.getElementById("lcn-threadstats-r"); }
#generatedStyle = null;
"writeCSSStyle" (origin, stylesheet) { "writeCSSStyle" (origin, stylesheet) {
if (this.#generatedStyle == null && (this.#generatedStyle = document.querySelector("head > style.generated-css")) == null) { if (this._generatedStyle == null && (this._generatedStyle = document.querySelector("head > style.generated-css")) == null) {
this.#generatedStyle = document.createElement("style") this._generatedStyle = document.createElement("style")
this.#generatedStyle.classList.add("generated-css") this._generatedStyle.classList.add("generated-css")
document.head.appendChild(this.#generatedStyle) document.head.appendChild(this._generatedStyle)
} }
this.#generatedStyle.textContent += `${this.#generatedStyle.textContent.length ? "\n\n" : ""}/*** Generated by ${origin} ***/\n${stylesheet}` this._generatedStyle.textContent += `${this._generatedStyle.textContent.length ? "\n\n" : ""}/*** Generated by ${origin} ***/\n${stylesheet}`
} }
} }
LCNSite.INSTANCE = null;
globalThis.LCNPostInfo = class LCNPostInfo { globalThis.LCNPostInfo = class LCNPostInfo {
static nodeAttrib = "$LCNPostInfo"; static "constructor" () {
static selector = ".post:not(.grid-li)"; this._boardId = null;
#boardId = null; this._threadId = null;
#threadId = null; this._postId = null;
#postId = null; this._name = null;
#name = null; this._email = null;
#email = null; this._capcode = null;
#capcode = null; this._flag = null;
#flag = null; this._ip = null;
#ip = null; this._subject = null;
#subject = null; this._createdAt = null;
#createdAt = null; this._parent = null;
this._isThread = false;
this._isReply = false;
this._isLocked = false;
this._isSticky = false;
}
#parent = null;
#isThread = false;
#isReply = false;
#isLocked = false;
#isSticky = false;
static "assign" (post) { return post[this.nodeAttrib] ?? (post[this.nodeAttrib] = this.from(post)); } static "assign" (post) { return post[this.nodeAttrib] ?? (post[this.nodeAttrib] = this.from(post)); }
static "from" (post) { static "from" (post) {
@ -147,353 +155,357 @@ globalThis.LCNPostInfo = class LCNPostInfo {
return inst return inst
} }
"getParent" () { return this.#parent; }
"__setParent" (inst) { return this.#parent = inst; }
"getBoardId" () { return this.#boardId; }
"getThreadId" () { return this.#threadId; }
"getPostId" () { return this.#postId; }
"getHref" () { return `/${this.boardId}/res/${this.threadId}.html#q${this.postId}`; }
"getName" () { return this.#name; }
"getEmail" () { return this.#email; }
"getIP" () { return this.#ip; }
"getCapcode" () { return this.#capcode; }
"getSubject" () { return this.#subject; }
"getCreatedAt" () { return this.#createdAt; }
"isSticky" () { return this.#isSticky; }
"isLocked" () { return this.#isLocked; }
"isThread" () { return this.#isThread; }
"isReply" () { return this.#isReply; }
"is" (info) {
assert.ok(info, "Must be LCNPost.")
return this.getBoardId() == info.getBoardId() && this.getPostId() == info.getPostId()
}
// "getParent" () { return this.#parent; }
// "__setParent" (inst) { return this.#parent = inst; }
//
// "getBoardId" () { return this.#boardId; }
// "getThreadId" () { return this.#threadId; }
// "getPostId" () { return this.#postId; }
// "getHref" () { return `/${this.boardId}/res/${this.threadId}.html#q${this.postId}`; }
//
// "getName" () { return this.#name; }
// "getEmail" () { return this.#email; }
// "getIP" () { return this.#ip; }
// "getCapcode" () { return this.#capcode; }
// "getSubject" () { return this.#subject; }
// "getCreatedAt" () { return this.#createdAt; }
//
// "isSticky" () { return this.#isSticky; }
// "isLocked" () { return this.#isLocked; }
// "isThread" () { return this.#isThread; }
// "isReply" () { return this.#isReply; }
//
// "is" (info) {
// assert.ok(info, "Must be LCNPost.")
// return this.getBoardId() == info.getBoardId() && this.getPostId() == info.getPostId()
// }
//
} }
globalThis.LCNPost = class LCNPost { LCNPostInfo.nodeAttrib = "$LCNPostInfo";
LCNPostInfo.selector = ".post:not(.grid-li)";
static nodeAttrib = "$LCNPost"; // globalThis.LCNPost = class LCNPost {
static selector = ".post:not(.grid-li)"; //
#parent = null; // static nodeAttrib = "$LCNPost";
#post = null; // static selector = ".post:not(.grid-li)";
#info = null; // #parent = null;
#ipLink = null; // #post = null;
#controls = null; // #info = null;
#customControlsSeperatorNode = null; // #ipLink = null;
// #controls = null;
static "assign" (post) { return post[this.nodeAttrib] ?? (post[this.nodeAttrib] = this.from(post)); } // #customControlsSeperatorNode = null;
static "from" (post) { return new this(post); } //
// static "assign" (post) { return post[this.nodeAttrib] ?? (post[this.nodeAttrib] = this.from(post)); }
"constructor" (post) { // static "from" (post) { return new this(post); }
assert.ok(post.classList.contains("post"), "Arty must be expected Element.") //
const intro = post.querySelector(".intro") // "constructor" (post) {
this.#post = post // assert.ok(post.classList.contains("post"), "Arty must be expected Element.")
this.#info = LCNPostInfo.assign(post) // const intro = post.querySelector(".intro")
this.#ipLink = intro.querySelector(".ip-link") // this.#post = post
this.#controls = Array.prototype.at.apply(post.querySelectorAll(".controls"), [ -1 ]) // this.#info = LCNPostInfo.assign(post)
// this.#ipLink = intro.querySelector(".ip-link")
assert.equal(this.#info.getParent(), null, "Info should not have parent.") // this.#controls = Array.prototype.at.apply(post.querySelectorAll(".controls"), [ -1 ])
this.#info.__setParent(this) //
} // assert.equal(this.#info.getParent(), null, "Info should not have parent.")
// this.#info.__setParent(this)
"jQuery" () { return $(this.#post); } // }
"trigger" (event_id, data=null) { $(this.#post).trigger(event_id, [ data ]); } //
// "jQuery" () { return $(this.#post); }
"getElement" () { return this.#post; } // "trigger" (event_id, data=null) { $(this.#post).trigger(event_id, [ data ]); }
"getInfo" () { return this.#info; } //
// "getElement" () { return this.#post; }
"getIPLink" () { return this.#ipLink; } // "getInfo" () { return this.#info; }
"setIP" (ip) { this.#ipLink.innerText = ip; } //
// "getIPLink" () { return this.#ipLink; }
"getParent" () { return this.#parent; } // "setIP" (ip) { this.#ipLink.innerText = ip; }
"__setParent" (inst) { return this.#parent = inst; } //
// "getParent" () { return this.#parent; }
static #NBSP = String.fromCharCode(160); // "__setParent" (inst) { return this.#parent = inst; }
"addCustomControl" (obj) { //
if (LCNSite.INSTANCE.isModerator()) { // static #NBSP = String.fromCharCode(160);
const link = document.createElement("a") // "addCustomControl" (obj) {
link.innerText = `[${obj.btn}]` // if (LCNSite.INSTANCE.isModerator()) {
link.title = obj.tooltip // const link = document.createElement("a")
// link.innerText = `[${obj.btn}]`
if (typeof obj.href == "string") { // link.title = obj.tooltip
link.href = obj.href //
link.referrerPolicy = "no-referrer" // if (typeof obj.href == "string") {
} else if (obj.onClick != undefined) { // link.href = obj.href
link.style.cursor = "pointer" // link.referrerPolicy = "no-referrer"
link.addEventListener("click", e => { e.preventDefault(); obj.onClick(this); }) // } else if (obj.onClick != undefined) {
} // link.style.cursor = "pointer"
// link.addEventListener("click", e => { e.preventDefault(); obj.onClick(this); })
if (this.#customControlsSeperatorNode == null) { // }
this.#controls.insertBefore(this.#customControlsSeperatorNode = new Text(`${this.constructor.#NBSP}-${this.constructor.#NBSP}`), this.#controls.firstElementChild) //
} else { // if (this.#customControlsSeperatorNode == null) {
this.#controls.insertBefore(new Text(this.constructor.#NBSP), this.#customControlsSeperatorNode) // this.#controls.insertBefore(this.#customControlsSeperatorNode = new Text(`${this.constructor.#NBSP}-${this.constructor.#NBSP}`), this.#controls.firstElementChild)
} // } else {
// this.#controls.insertBefore(new Text(this.constructor.#NBSP), this.#customControlsSeperatorNode)
this.#controls.insertBefore(link, this.#customControlsSeperatorNode) // }
} //
} // this.#controls.insertBefore(link, this.#customControlsSeperatorNode)
// }
} // }
//
globalThis.LCNThread = class LCNThread { // }
//
static nodeAttrib = "$LCNThread"; // globalThis.LCNThread = class LCNThread {
static selector = ".thread:not(.grid-li)"; //
#element = null; // static nodeAttrib = "$LCNThread";
#parent = null; // static selector = ".thread:not(.grid-li)";
#op = null; // #element = null;
// #parent = null;
static "assign" (thread) { return thread[this.nodeAttrib] ?? (thread[this.nodeAttrib] = this.from(thread)); } // #op = null;
static "from" (thread) { return new this(thread); } //
// static "assign" (thread) { return thread[this.nodeAttrib] ?? (thread[this.nodeAttrib] = this.from(thread)); }
"constructor" (thread) { // static "from" (thread) { return new this(thread); }
assert.ok(thread.classList.contains("thread"), "Arty must be expected Element.") //
this.#element = thread // "constructor" (thread) {
this.#op = LCNPost.assign(this.#element.querySelector(".post.op")) // assert.ok(thread.classList.contains("thread"), "Arty must be expected Element.")
// this.#element = thread
//assert.equal(this.#op.getParent(), null, "Op should not have parent.") // this.#op = LCNPost.assign(this.#element.querySelector(".post.op"))
this.#op.__setParent(this) //
} // //assert.equal(this.#op.getParent(), null, "Op should not have parent.")
// this.#op.__setParent(this)
"getElement" () { return this.#element; } // }
"getContent" () { return this.#op; } //
"getPosts" () { return Array.prototype.map.apply(this.#element.querySelectorAll(".post"), [ el => LCNPost.assign(el) ]); } // "getElement" () { return this.#element; }
"getReplies" () { return Array.prototype.map.apply(this.#element.querySelectorAll(".post:not(.op)"), [ el => LCNPost.assign(el) ]); } // "getContent" () { return this.#op; }
// "getPosts" () { return Array.prototype.map.apply(this.#element.querySelectorAll(".post"), [ el => LCNPost.assign(el) ]); }
"getParent" () { return this.#parent; } // "getReplies" () { return Array.prototype.map.apply(this.#element.querySelectorAll(".post:not(.op)"), [ el => LCNPost.assign(el) ]); }
"__setParent" (inst) { return this.#parent = inst; } //
} // "getParent" () { return this.#parent; }
// "__setParent" (inst) { return this.#parent = inst; }
// }
globalThis.LCNPostContainer = class LCNPostContainer { //
//
static nodeAttrib = "$LCNPostContainer"; // globalThis.LCNPostContainer = class LCNPostContainer {
static selector = ".postcontainer"; //
#parent = null; // static nodeAttrib = "$LCNPostContainer";
#element = null; // static selector = ".postcontainer";
#content = null; // #parent = null;
#postId = null; // #element = null;
#boardId = null; // #content = null;
// #postId = null;
static "assign" (container) { return container[this.nodeAttrib] ?? (container[this.nodeAttrib] = this.from(container)); } // #boardId = null;
static "from" (container) { return new this(container); } //
// static "assign" (container) { return container[this.nodeAttrib] ?? (container[this.nodeAttrib] = this.from(container)); }
"constructor" (container) { // static "from" (container) { return new this(container); }
assert.ok(container.classList.contains("postcontainer"), "Arty must be expected Element.") //
const child = container.querySelector(".thread, .post") // "constructor" (container) {
this.#element = container // assert.ok(container.classList.contains("postcontainer"), "Arty must be expected Element.")
this.#content = child.classList.contains("thread") ? LCNThread.assign(child) : LCNPost.assign(child) // const child = container.querySelector(".thread, .post")
this.#boardId = container.dataset.board // this.#element = container
this.#postId = container.id.slice(2) // this.#content = child.classList.contains("thread") ? LCNThread.assign(child) : LCNPost.assign(child)
// this.#boardId = container.dataset.board
assert.equal(this.#content.getParent(), null, "Content should not have parent.") // this.#postId = container.id.slice(2)
this.#content.__setParent(this) //
} // assert.equal(this.#content.getParent(), null, "Content should not have parent.")
// this.#content.__setParent(this)
"getElement" () { return this.#element; } // }
"getContent" () { return this.#content; } //
"getBoardId" () { return this.#boardId; } // "getElement" () { return this.#element; }
"getPostId" () { return this.#postId; } // "getContent" () { return this.#content; }
// "getBoardId" () { return this.#boardId; }
"getParent" () { return this.#parent; } // "getPostId" () { return this.#postId; }
"__setParent" (inst) { return this.#parent = inst; } //
// "getParent" () { return this.#parent; }
} // "__setParent" (inst) { return this.#parent = inst; }
//
globalThis.LCNPostWrapper = class LCNPostWrapper { // }
//
static nodeAttrib = "$LCNPostWrapper"; // globalThis.LCNPostWrapper = class LCNPostWrapper {
static selector = ".post-wrapper"; //
#wrapper = null; // static nodeAttrib = "$LCNPostWrapper";
#eitaLink = null; // static selector = ".post-wrapper";
#eitaId = null; // #wrapper = null;
#eitaHref = null // #eitaLink = null;
#content = null; // #eitaId = null;
// #eitaHref = null
static "assign" (wrapper) { return wrapper[this.nodeAttrib] ?? (wrapper[this.nodeAttrib] = this.from(wrapper)); } // #content = null;
static "from" (wrapper) { return new this(wrapper); } //
// static "assign" (wrapper) { return wrapper[this.nodeAttrib] ?? (wrapper[this.nodeAttrib] = this.from(wrapper)); }
"constructor" (wrapper) { // static "from" (wrapper) { return new this(wrapper); }
assert.ok(wrapper.classList.contains("post-wrapper"), "Arty must be expected Element.") //
this.#wrapper = wrapper // "constructor" (wrapper) {
this.#eitaLink = wrapper.querySelector(".eita-link") // assert.ok(wrapper.classList.contains("post-wrapper"), "Arty must be expected Element.")
this.#eitaId = this.#eitaLink.id // this.#wrapper = wrapper
this.#eitaHref = this.#eitaLink.href // this.#eitaLink = wrapper.querySelector(".eita-link")
void Array.prototype.find.apply(wrapper.children, [ // this.#eitaId = this.#eitaLink.id
el => { // this.#eitaHref = this.#eitaLink.href
if (el.classList.contains("thread")) { // void Array.prototype.find.apply(wrapper.children, [
return this.#content = LCNThread.assign(el) // el => {
} else if (el.classList.contains("postcontainer")) { // if (el.classList.contains("thread")) {
return this.#content = LCNPostContainer.assign(el) // return this.#content = LCNThread.assign(el)
} // } else if (el.classList.contains("postcontainer")) {
} // return this.#content = LCNPostContainer.assign(el)
]) // }
// }
assert.ok(this.#content, "Wrapper should contain content.") // ])
assert.equal(this.#content.getParent(), null, "Content should not have parent.") //
this.#content.__setParent(this) // assert.ok(this.#content, "Wrapper should contain content.")
} // assert.equal(this.#content.getParent(), null, "Content should not have parent.")
// this.#content.__setParent(this)
"getPost" () { // }
const post = this.getContent().getContent() //
assert.ok(post instanceof LCNPost, "Post should be LCNPost.") // "getPost" () {
return post // const post = this.getContent().getContent()
} // assert.ok(post instanceof LCNPost, "Post should be LCNPost.")
// return post
"getElement" () { return this.#wrapper; } // }
"getContent" () { return this.#content; } //
"getEitaId" () { return this.#eitaId; } // "getElement" () { return this.#wrapper; }
"getEitaHref" () { return this.#eitaHref; } // "getContent" () { return this.#content; }
"getEitaLink" () { return this.#eitaLink; } // "getEitaId" () { return this.#eitaId; }
// "getEitaHref" () { return this.#eitaHref; }
} // "getEitaLink" () { return this.#eitaLink; }
//
globalThis.LCNSetting = class LCNSetting { // }
#id = null; //
#eventId = null; // globalThis.LCNSetting = class LCNSetting {
#label = null; // #id = null;
#value = null; // #eventId = null;
#valueDefault = null; // #label = null;
// #value = null;
static "build" (id) { return new this(id); } // #valueDefault = null;
//
"constructor" (id) { // static "build" (id) { return new this(id); }
this.#id = id; //
this.#eventId = `lcnsetting::${this.#id}` // "constructor" (id) {
} // this.#id = id;
// this.#eventId = `lcnsetting::${this.#id}`
#getValue () { // }
const v = localStorage.getItem(this.#id) //
if (v != null) { // #getValue () {
return this.__builtinValueImporter(v) // const v = localStorage.getItem(this.#id)
} else { // if (v != null) {
return this.#valueDefault // return this.__builtinValueImporter(v)
} // } else {
} // return this.#valueDefault
// }
"getValue" () { return this.#value ?? (this.#value = this.#getValue()); } // }
"setValue" (v) { //
if (this.#value !== v) { // "getValue" () { return this.#value ?? (this.#value = this.#getValue()); }
this.#value = v // "setValue" (v) {
localStorage.setItem(this.#id, this.__builtinValueExporter(this.#value)) // if (this.#value !== v) {
setTimeout(() => $(document).trigger(`${this.#eventId}::change`, [ v, this ]), 1) // this.#value = v
} // localStorage.setItem(this.#id, this.__builtinValueExporter(this.#value))
} // setTimeout(() => $(document).trigger(`${this.#eventId}::change`, [ v, this ]), 1)
// }
"getLabel" () { return this.#label; } // }
"setLabel" (label) { this.#label = label; return this; } //
// "getLabel" () { return this.#label; }
"getDefaultValue" () { return this.#valueDefault; } // "setLabel" (label) { this.#label = label; return this; }
"setDefaultValue" (vd) { this.#valueDefault = vd; return this; } //
// "getDefaultValue" () { return this.#valueDefault; }
"onChange" (fn) { $(document).on(`${this.#eventId}::change`, (_,v,i) => fn(v, i)); } // "setDefaultValue" (vd) { this.#valueDefault = vd; return this; }
__setIdPrefix (prefix) { this.#id = `${prefix}_${this.#id}`; } //
} // "onChange" (fn) { $(document).on(`${this.#eventId}::change`, (_,v,i) => fn(v, i)); }
// __setIdPrefix (prefix) { this.#id = `${prefix}_${this.#id}`; }
globalThis.LCNToggleSetting = class LCNToggleSetting extends LCNSetting { // }
__builtinValueImporter (v) { return v == "1"; } //
__builtinValueExporter (v) { return v ? "1" : ""; } // globalThis.LCNToggleSetting = class LCNToggleSetting extends LCNSetting {
__builtinDOMConstructor () { // __builtinValueImporter (v) { return v == "1"; }
const div = document.createElement("div") // __builtinValueExporter (v) { return v ? "1" : ""; }
const chk = document.createElement("input") // __builtinDOMConstructor () {
const txt = document.createElement("label") // const div = document.createElement("div")
txt.innerText = this.getLabel() // const chk = document.createElement("input")
chk.type = "checkbox" // const txt = document.createElement("label")
chk.checked = this.getValue() // txt.innerText = this.getLabel()
chk.addEventListener("click", e => { // chk.type = "checkbox"
e.preventDefault(); // chk.checked = this.getValue()
this.setValue(!this.getValue()) // chk.addEventListener("click", e => {
}) // e.preventDefault();
this.onChange(v => chk.checked = v) // this.setValue(!this.getValue())
// })
div.appendChild(chk) // this.onChange(v => chk.checked = v)
div.appendChild(txt) //
return div // div.appendChild(chk)
} // div.appendChild(txt)
} // return div
// }
globalThis.LCNSettingsSubcategory = class LCNSettingsSubcategory { // }
//
#tab_id = null; // globalThis.LCNSettingsSubcategory = class LCNSettingsSubcategory {
#id = null; //
// #tab_id = null;
#fieldset = null; // #id = null;
#legend = null; //
#label = null; // #fieldset = null;
// #legend = null;
static "for" (tab_id, id) { // #label = null;
const domid = `lcnssc_${tab_id}_${id}` //
const inst = document.getElementById(domid)?.$LCNSettingsSubcategory // static "for" (tab_id, id) {
if (inst == null) { // const domid = `lcnssc_${tab_id}_${id}`
const fieldset = document.createElement("fieldset") // const inst = document.getElementById(domid)?.$LCNSettingsSubcategory
const legend = document.createElement("legend") // if (inst == null) {
fieldset.id = domid // const fieldset = document.createElement("fieldset")
fieldset.appendChild(legend) // const legend = document.createElement("legend")
// fieldset.id = domid
// XXX: extend_tab only takes a string so this hacky workaround is used to let us use the regular dom api // fieldset.appendChild(legend)
Options.extend_tab(tab_id, `<div id="__${domid}" hidden></div>`) //
const div = document.getElementById(`__${domid}`)?.parentElement // // XXX: extend_tab only takes a string so this hacky workaround is used to let us use the regular dom api
assert.ok(div) // Options.extend_tab(tab_id, `<div id="__${domid}" hidden></div>`)
// const div = document.getElementById(`__${domid}`)?.parentElement
div.replaceChildren(fieldset) // assert.ok(div)
return new this(tab_id, id, fieldset) //
} else { // div.replaceChildren(fieldset)
return inst // return new this(tab_id, id, fieldset)
} // } else {
} // return inst
// }
"constructor" (tab_id, id, fieldset) { // }
this.#tab_id = tab_id //
this.#id = id // "constructor" (tab_id, id, fieldset) {
this.#fieldset = fieldset // this.#tab_id = tab_id
this.#legend = this.#fieldset.querySelector("legend") // this.#id = id
this.#fieldset.$LCNSettingsSubcategory = this // this.#fieldset = fieldset
} // this.#legend = this.#fieldset.querySelector("legend")
// this.#fieldset.$LCNSettingsSubcategory = this
"getLabel" () { return this.#label; } // }
"setLabel" (label) { this.#legend.innerText = this.#label = label; return this; } //
"addSetting" (setting) { // "getLabel" () { return this.#label; }
assert.ok(setting instanceof LCNSetting) // "setLabel" (label) { this.#legend.innerText = this.#label = label; return this; }
setting.__setIdPrefix(`lcnsetting_${this.#tab_id}_${this.#id}`) // "addSetting" (setting) {
if (setting.__builtinDOMConstructor != null) { // assert.ok(setting instanceof LCNSetting)
const div = setting.__builtinDOMConstructor() // setting.__setIdPrefix(`lcnsetting_${this.#tab_id}_${this.#id}`)
div.classList.add("lcn-setting-entry") // if (setting.__builtinDOMConstructor != null) {
this.#fieldset.appendChild(div) // const div = setting.__builtinDOMConstructor()
} // div.classList.add("lcn-setting-entry")
// this.#fieldset.appendChild(div)
return this // }
} //
// return this
} // }
//
$().ready(() => { // }
LCNSite.INSTANCE = new LCNSite(); //
// $().ready(() => {
for (const clazz of [ LCNPost, LCNPostInfo, LCNThread, LCNPostContainer, LCNPostWrapper ]) { // LCNSite.INSTANCE = new LCNSite();
clazz.allNodes = (node=document) => node.querySelectorAll(clazz.selector) //
clazz.all = (node=document) => Array.prototype.map.apply(clazz.allNodes(node), [ elem => clazz.assign(elem) ]); // for (const clazz of [ LCNPost, LCNPostInfo, LCNThread, LCNPostContainer, LCNPostWrapper ]) {
clazz.clear = (node=document) => Array.prototype.forEach.apply(clazz.allNodes(node), [ elem => elem[clazz.nodeAttrib] = null ]) // clazz.allNodes = (node=document) => node.querySelectorAll(clazz.selector)
clazz.forEach = (fn, node=document) => clazz.allNodes(node).forEach(elem => fn(clazz.assign(elem))) // clazz.all = (node=document) => Array.prototype.map.apply(clazz.allNodes(node), [ elem => clazz.assign(elem) ]);
clazz.filter = (fn, node=document) => clazz.all(node).filter(fn) // clazz.clear = (node=document) => Array.prototype.forEach.apply(clazz.allNodes(node), [ elem => elem[clazz.nodeAttrib] = null ])
clazz.find = fn => clazz.all().find(fn) // clazz.forEach = (fn, node=document) => clazz.allNodes(node).forEach(elem => fn(clazz.assign(elem)))
clazz.first = (node=document) => clazz.assign(node.querySelector(clazz.selector)) // clazz.filter = (fn, node=document) => clazz.all(node).filter(fn)
clazz.last = (node=document) => clazz.assign(Array.prototype.at.apply(clazz.allNodes(node), [ -1 ])) // clazz.find = fn => clazz.all().find(fn)
} // clazz.first = (node=document) => clazz.assign(node.querySelector(clazz.selector))
// clazz.last = (node=document) => clazz.assign(Array.prototype.at.apply(clazz.allNodes(node), [ -1 ]))
// XXX: May be a cleaner way to do this but this should be fine for now. // }
for (const clazz of [ LCNPostContainer, LCNPostWrapper, LCNThread, LCNPost ]) { void clazz.all(); } //
$(document).on("new_post", (e, post) => { // // XXX: May be a cleaner way to do this but this should be fine for now.
if (LCNSite.INSTANCE.isModRecentsPage()) { // for (const clazz of [ LCNPostContainer, LCNPostWrapper, LCNThread, LCNPost ]) { void clazz.all(); }
void LCNPostWrapper.all() // $(document).on("new_post", (e, post) => {
} else { // if (LCNSite.INSTANCE.isModRecentsPage()) {
void LCNPostContainer.all() // void LCNPostWrapper.all()
} // } else {
}) // void LCNPostContainer.all()
// }
$(window).on("focus", () => LCNSite.INSTANCE.clearUnseen()) // })
$(document.body).on("mousemove", () => LCNSite.INSTANCE.clearUnseen()) //
}) // $(window).on("focus", () => LCNSite.INSTANCE.clearUnseen())
// $(document.body).on("mousemove", () => LCNSite.INSTANCE.clearUnseen())
// })

View File

@ -35,7 +35,7 @@ $().ready(() => {
} }
const updateSecondsByTSLP = post_info => { const updateSecondsByTSLP = post_info => {
secondsCounter = Math.floor(((Date.now() - post_info.getCreatedAt().getTime()) / 30000)) secondsCounter = Math.floor(((Date.now() - post_info.getCreatedAt().getTime()) / 120000))
secondsCounter = secondsCounter > 1000 ? 1000 : secondsCounter secondsCounter = secondsCounter > 1000 ? 1000 : secondsCounter
secondsCounter = secondsCounter < 11 ? 11 : secondsCounter secondsCounter = secondsCounter < 11 ? 11 : secondsCounter
} }
@ -58,43 +58,24 @@ $().ready(() => {
} }
} }
const handleThreadUpdate = async (thread) => { const findMissingReplies = (thread_op, thread_dom, thread_latest) => {
const threadPost = thread.getContent() const lastPostTs = (thread_dom.at(-1)?.getInfo() ?? thread_op).getCreatedAt().getTime()
const missing = []
const res = await fetch(location.href, { for (const pc of thread_latest.reverse()) {
"signal": abortable.signal
})
if (res.ok) {
const dom = parser.parseFromString(await res.text(), "text/html")
const livePCList = Array.prototype.map.apply(dom.querySelectorAll(`#thread_${threadPost.getInfo().getThreadId()} > .postcontainer`), [ pc => LCNPostContainer.assign(pc) ])
updateThreadFn(thread, livePCList);
} else if (res.status == 404) {
threadState = String(res.status)
} else {
throw new Error(`Server responded with non-OK status '${res.status}'`)
}
}
function updateThreadFn(thread, lcn_pc_list) {
const threadPost = thread.getContent()
const threadReplies = thread.getReplies()
const lastPostC = threadReplies.at(-1).getParent()
const lastPostTs = lastPostC.getContent().getInfo().getCreatedAt().getTime()
const livePCList = lcn_pc_list;
const documentPCList = [ threadPost, ...threadReplies.map(p => p.getParent()) ]
const missingPCList = []
for (const pc of livePCList.reverse()) {
if (pc.getContent().getInfo().getCreatedAt().getTime() > lastPostTs) { if (pc.getContent().getInfo().getCreatedAt().getTime() > lastPostTs) {
missingPCList.unshift(pc) missing.unshift(pc)
} else { } else {
break break
} }
} }
return missing
}
const updateRepliesFn = (thread, missingPCList) => {
if (missingPCList.length) { if (missingPCList.length) {
const documentPCList = [ thread.getContent(), ...(thread.getReplies()).map(p => p.getParent()) ]
for (const pc of missingPCList) { for (const pc of missingPCList) {
documentPCList.at(-1).getElement().after(pc.getElement()) documentPCList.at(-1).getElement().after(pc.getElement())
documentPCList.push(pc) documentPCList.push(pc)
@ -106,7 +87,29 @@ $().ready(() => {
LCNSite.INSTANCE.setUnseen(LCNSite.INSTANCE.getUnseen() + missingPCList.length) LCNSite.INSTANCE.setUnseen(LCNSite.INSTANCE.getUnseen() + missingPCList.length)
} }
}
const updateThreadFn = async (thread, dom) => {
const threadPost = thread.getContent()
const threadReplies = thread.getReplies()
const missingPCList = findMissingReplies(
threadPost,
threadReplies,
LCNPostContainer.all(dom.querySelector(`#thread_${threadPost.getInfo().getThreadId()}`)))
updateRepliesFn(thread, missingPCList)
}
const fetchThreadFn = async () => {
const res = await fetch(location.href, { "signal": abortable.signal })
if (res.ok) {
return parser.parseFromString(await res.text(), "text/html")
} else {
if (res.status == 404) {
threadState = String(res.status)
}
throw new Error(`Server responded with non-OK status '${res.status}'`)
}
} }
const onTickClean = () => { const onTickClean = () => {
@ -129,7 +132,7 @@ $().ready(() => {
try { try {
await updateStatsFn(thread) await updateStatsFn(thread)
if (threadState == null && threadStats.last_modified > (thread.getReplies().at(-1).getInfo().getCreatedAt().getTime() / 1000)) { if (threadState == null && threadStats.last_modified > (thread.getReplies().at(-1).getInfo().getCreatedAt().getTime() / 1000)) {
await handleThreadUpdate(thread) updateThreadFn(thread, await fetchThreadFn())
} }
const threadEl = thread.getElement() const threadEl = thread.getElement()
@ -148,6 +151,26 @@ $().ready(() => {
} }
} }
$(document).on("ajax_after_post", (_, xhr_body) => {
if (kIsEnabled.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())
} else {
$(document).trigger("thread_manual_refresh")
}
}
})
$(document).on("thread_manual_refresh", () => {
if (kIsEnabled.getValue() && secondsCounter >= 0) {
secondsCounter = 0
onTickFn()
}
})
let floaterLinkBox = null let floaterLinkBox = null
const onStateChangeFn = v => { const onStateChangeFn = v => {
onTickClean() onTickClean()
@ -164,10 +187,7 @@ $().ready(() => {
threadUpdateStatus.innerText = "…" threadUpdateStatus.innerText = "…"
threadUpdateLink.addEventListener("click", e => { threadUpdateLink.addEventListener("click", e => {
e.preventDefault() e.preventDefault()
if (secondsCounter >= 0) { $(document).trigger("thread_manual_refresh")
secondsCounter = 0
onTickFn()
}
}) })
threadUpdateLink.href = "#" threadUpdateLink.href = "#"
threadUpdateLink.appendChild(new Text("Refresh: ")) threadUpdateLink.appendChild(new Text("Refresh: "))
@ -211,8 +231,7 @@ $().ready(() => {
} }
} }
secondsCounter = 0 $(document).trigger("thread_manual_refresh")
setTimeout(onTickFn, 1)
} else { } else {
floaterLinkBox?.remove() floaterLinkBox?.remove()
floaterLinkBox = null floaterLinkBox = null
@ -228,26 +247,5 @@ $().ready(() => {
kIsEnabled.onChange(onStateChangeFn) kIsEnabled.onChange(onStateChangeFn)
onStateChangeFn(kIsEnabled.getValue()) onStateChangeFn(kIsEnabled.getValue())
$(document).on("ajax_after_post", onNewPost);
function onNewPost(_, post_response) {
if (post_response == null) {
console.log("onNewPost data is null, can't do anything.");
return;
}
const thread_dom = parser.parseFromString(
post_response['thread'],
"text/html");
const thread_id_sel = "#thread_" + post_response['thread_id'];
const post_containers = [...thread_dom.querySelectorAll(`${thread_id_sel} > .postcontainer`)]
.map(elem => LCNPostContainer.assign(elem));
const thread_elem = document.querySelector(thread_id_sel);
const lcn_thread = new LCNThread(thread_elem);
updateThreadFn(lcn_thread, post_containers);
}
} }
}) })

View File

@ -6,37 +6,38 @@
const assert = { const assert = {
"equal": (actual, expected, message="No message set") => { "equal": (actual, expected, message="No message set") => {
if (actual !== expected) { if (actual !== expected) {
const err = new Error(`Assertion Failed. ${message}`) const err = new Error(`Assertion Failed. ${message}`);
err.data = { actual, expected} err.data = { actual, expected}
// Seems like there's no such thing as captureStackTrace in firefox? //Error.captureStackTrace?.(err, assert.equal);
//Error.captureStackTrace(err, assert.equal) debugger;
debugger throw err;
throw err
} }
}, },
"ok": (actual, message="No message set") => { "ok": (actual, message="No message set") => {
if (!actual) { if (!actual) {
const err = new Error(`Assertion Failed. ${message}`) const err = new Error(`Assertion Failed. ${message}`);
err.data = { actual } err.data = { actual }
// Error.captureStackTrace(err, assert.ok) //Error.captureStackTrace?.(err, assert.ok);
debugger debugger;
throw err throw err;
} }
} }
} };
if (AbortSignal.any == null) {
AbortSignal.any = (signals) => {
const controller = new AbortController();
const abortFn = () => {
for (const signal of signals) {
signal.removeEventListener("abort", abortFn);
}
controller.abort();
}
AbortSignal.any ??= function (signals) {
const controller = new AbortController()
const abortFn = () => {
for (const signal of signals) { for (const signal of signals) {
signal.removeEventListener("abort", abortFn) signal.addEventListener("abort", abortFn);
} }
controller.abort()
}
for (const signal of signals) { return controller.signal;
signal.addEventListener("abort", abortFn)
} }
return controller.signal
} }

View File

@ -1495,7 +1495,8 @@ function handle_post(){
'noko' => $noko, 'noko' => $noko,
'id' => $id, 'id' => $id,
'thread_id' => $thread_id, 'thread_id' => $thread_id,
'thread' => $rendered_thread 'thread' => $rendered_thread,
'mod' => !!$post['mod']
)); ));
} }