types and small improvements

This commit is contained in:
Lalle 2023-04-28 06:01:40 +02:00
parent 329fc4dd14
commit 69051d6095
No known key found for this signature in database
GPG Key ID: A6583D207A8F6B0D
4 changed files with 65 additions and 71 deletions

View File

@ -0,0 +1 @@
Only classes

View File

@ -11,7 +11,7 @@ export default class Post {
declare root: HTMLElement
declare thread: Thread
declare board: Board
declare ID: number
declare ID: number | string
declare postID: number
declare threadID: number
declare boardID: number | string
@ -22,7 +22,7 @@ export default class Post {
declare nodes: ReturnType<Post['parseNodes']>
declare isDead: boolean
declare isHidden: boolean
declare clones: any[]
declare clones: unknown[]
declare isRebuilt?: boolean
declare isFetchedQuote: boolean
declare isClone: boolean
@ -31,17 +31,17 @@ export default class Post {
declare files: ReturnType<Post['parseFile']>[]
declare info: {
subject: string | undefined,
name: string | undefined,
email: string | undefined,
tripcode: string | undefined,
uniqueID: string | undefined,
capcode: string | undefined,
pass: string | undefined,
flagCode: string | undefined,
flagCodeTroll: string | undefined,
flag: string | undefined,
date: Date | undefined,
subject: string,
name: string,
email: string,
tripcode: string,
uniqueID: string,
capcode: string,
pass: string,
flagCode: string,
flagCodeTroll: string,
flag: string,
date: Date,
nameBlock: string,
}
@ -85,7 +85,7 @@ export default class Post {
if (!this.isReply) {
this.thread.OP = this
for (const key of ['isSticky', 'isClosed', 'isArchived']) {
var selector
var selector: any
if (selector = g.SITE.selectors.icons[key]) {
this.thread[key] = !!$(selector, this.nodes.info)
}
@ -135,8 +135,8 @@ export default class Post {
}
if (!this.isFetchedQuote && (this.ID > this.thread.lastPost)) { this.thread.lastPost = this.ID }
this.board.posts.push(this.ID, this)
this.thread.posts.push(this.ID, this)
this.board.posts.push(this.ID.toString(), this)
this.thread.posts.push(this.ID.toString(), this)
g.posts.push(this.fullID, this)
this.isFetchedQuote = false
@ -187,7 +187,7 @@ export default class Post {
parseComment() {
// Merge text nodes and remove empty ones.
let bq
let bq: Node
this.nodes.comment.normalize()
// Get the comment's text.
@ -221,8 +221,8 @@ export default class Post {
return this.nodesToText(bq)
}
nodesToText(bq) {
let node
nodesToText(bq: any) {
let node: Node
let text = ""
const nodes = $.X('.//br|.//text()', bq)
let i = 0
@ -232,7 +232,7 @@ export default class Post {
return text
}
cleanSpoilers(bq) {
cleanSpoilers(bq: HTMLElement) {
const spoilers = $$(g.SITE.selectors.spoiler, bq)
for (const node of spoilers) {
$.replace(node, $.tn('[spoiler]'))
@ -246,7 +246,7 @@ export default class Post {
}
}
parseQuote(quotelink) {
parseQuote(quotelink: HTMLAnchorElement) {
// Only add quotes that link to posts on an imageboard.
// Don't add:
// - board links. (>>>/b/)
@ -326,8 +326,8 @@ export default class Post {
return file as File
}
kill(file, index = 0) {
let strong
kill(file: any, index = 0) {
let strong: { textContent: string }
if (file) {
if (this.isDead || this.files[index].isDead) { return }
this.files[index].isDead = true
@ -395,20 +395,20 @@ export default class Post {
this.board.posts.rm(this)
}
addClone(context, contractThumb) {
addClone(context: any, contractThumb: any) {
// Callbacks may not have been run yet due to anti-browser-lock delay in Main.callbackNodesDB.
Callbacks.Post.execute(this)
return new PostClone(this, context, contractThumb)
}
rmClone(index) {
rmClone(index: number) {
this.clones.splice(index, 1)
for (const clone of this.clones.slice(index)) {
clone.nodes.root.dataset.clone = index++
}
}
setCatalogOP(isCatalogOP) {
setCatalogOP(isCatalogOP: boolean) {
this.nodes.root.classList.toggle('catalog-container', isCatalogOP)
this.nodes.root.classList.toggle('opContainer', !isCatalogOP)
this.nodes.post.classList.toggle('catalog-post', isCatalogOP)
@ -422,11 +422,11 @@ export class PostClone extends Post {
static suffix = 0
constructor(origin, context, contractThumb) {
constructor(origin: Post, context: Post, contractThumb: any) {
super()
this.isClone = true
let file, fileRoots, key
let file, fileRoots: any[], key: string | number
this.origin = origin
this.context = context
for (key of ['ID', 'postID', 'threadID', 'boardID', 'siteID', 'fullID', 'board', 'thread', 'info', 'quotes', 'isReply']) {
@ -458,9 +458,9 @@ export class PostClone extends Post {
// Remove catalog stuff.
if (!this.isReply) {
this.setCatalogOP(false)
$.rm($('.catalog-link', this.nodes.post))
$.rm($('.catalog-stats', this.nodes.post))
$.rm($('.catalog-replies', this.nodes.post))
$.rm($('.catalog-link')),
$.rm($('.catalog-stats')),
$.rm($('.catalog-subject'))
}
this.parseQuotes()
@ -495,7 +495,7 @@ export class PostClone extends Post {
return this
}
cloneWithoutVideo(node) {
cloneWithoutVideo(node: HTMLElement) {
if ((node.tagName === 'VIDEO') && !node.dataset.md5) { // (exception for WebM thumbnails)
return []
} else if ((node.nodeType === Node.ELEMENT_NODE) && $('video', node)) {

View File

@ -1,10 +1,4 @@
import { d } from "../globals/globals"
/*
* decaffeinate suggestions:
* DS101: Remove unnecessary use of Array.from
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const $$ = (selector, root = d.body) => [...Array.from(root.querySelectorAll(selector))]
export default $$

View File

@ -8,6 +8,7 @@
// loosely follows the jquery api:
// http://api.jquery.com/
import Callbacks from "../classes/Callbacks"
import Notice from "../classes/Notice"
import { c, Conf, d, doc, g } from "../globals/globals"
import CrossOrigin from "./CrossOrigin"
@ -17,7 +18,16 @@ import { debounce, dict, MINUTE, platform, SECOND } from "./helpers"
const $ = (selector, root = document.body) => root.querySelector(selector)
$.id = id => d.getElementById(id)
$.cache = (function () {
const cache = {}
return function (key, value) {
if (value != null) {
return (cache[key] = value)
} else {
return cache[key]
}
}
})()
$.ajaxPage = function (url, options) {
if (options.responseType == null) { options.responseType = 'json' }
if (!options.type) { options.type = (options.form && 'post') || 'get' }
@ -88,7 +98,7 @@ $.ajax = (function () {
pageXHR = XMLHttpRequest
}
const r = (function (url, options = {}) {
const r = (function (url: string, options) {
if (options.responseType == null) { options.responseType = 'json' }
if (!options.type) { options.type = (options.form && 'post') || 'get' }
// XXX https://forums.lanik.us/viewtopic.php?f=64&t=24173&p=78310
@ -321,7 +331,7 @@ $.onExists = function (root, selector, cb) {
if (el = $(selector, root)) {
return cb(el)
}
var observer = new MutationObserver(function () {
const observer = new MutationObserver(function () {
if (el = $(selector, root)) {
observer.disconnect()
return cb(el)
@ -508,31 +518,15 @@ $.debounce = function (wait, fn) {
}
}
$.queueTask = (function () {
// inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
const taskQueue = []
const execTask = function () {
const task = taskQueue.shift()
const func = task[0]
const args = Array.prototype.slice.call(task, 1)
return func.apply(func, args)
}
if (window.MessageChannel) {
const taskChannel = new MessageChannel()
taskChannel.port1.onmessage = execTask
return function () {
taskQueue.push(arguments)
return taskChannel.port2.postMessage(null)
}
} else { // XXX Firefox
return function () {
taskQueue.push(arguments)
return setTimeout(execTask, 0)
$.queueTask = function (fn) {
if (typeof requestIdleCallback === 'function') {
return requestIdleCallback(fn)
} else {
return setTimeout(fn, 0)
}
}
})()
$.global = function (fn, data) {
$.global = function (fn, data?) {
if (doc) {
const script = $.el('script',
{ textContent: `(${fn}).call(document.currentScript.dataset);` })
@ -645,8 +639,13 @@ if (platform === 'crx') {
}
})
$.sync = (key, cb) => $.syncing[key] = cb
$.forceSync = function () { }
$.forceSync = function (key, cb) {
chrome.storage.sync.get(key, function (data) {
if (data[key] != null) {
cb(data[key], key)
}
})
}
$.crxWorking = function () {
try {
if (chrome.runtime.getManifest()) {
@ -998,15 +997,15 @@ if (platform === 'crx') {
})
})
$.clear = function (cb) {
$.clear = function (cb: Callbacks) {
// XXX https://github.com/greasemonkey/greasemonkey/issues/2033
// Also support case where GM_listValues is not defined.
$.delete(Object.keys(Conf))
$.delete(['previousversion', 'QR Size', 'QR.persona'])
$.delete(Object.keys(Conf), cb)
$.delete(['previousversion', 'QR Size', 'QR.persona'], cb)
try {
$.delete($.listValues().map(key => key.replace(g.NAMESPACE, '')))
$.delete($.listValues().map(key => key.replace(g.NAMESPACE, '')), cb)
} catch (error) { }
return cb?.()
return cb
}
}
}