add func and bug fixes

This commit is contained in:
Lalle 2023-04-19 05:22:16 +02:00
parent 0eb1f61d43
commit f3b9ecdae9
No known key found for this signature in database
GPG Key ID: A6583D207A8F6B0D
2 changed files with 45 additions and 21 deletions

View File

@ -115,7 +115,7 @@ export default class DataBoard {
return return
} }
delete this.data[siteID].boards[boardID][threadID] delete this.data[siteID].boards[boardID][threadID]
return this.deleteIfEmpty({ siteID, boardID }) return this.deleteIfEmpty({ siteID, boardID, threadID })
} else { } else {
return delete this.data[siteID].boards[boardID] return delete this.data[siteID].boards[boardID]
} }
@ -129,7 +129,7 @@ export default class DataBoard {
if (threadID) { if (threadID) {
if (!Object.keys(this.data[siteID].boards[boardID][threadID]).length) { if (!Object.keys(this.data[siteID].boards[boardID][threadID]).length) {
delete this.data[siteID].boards[boardID][threadID] delete this.data[siteID].boards[boardID][threadID]
return this.deleteIfEmpty({ siteID, boardID }) return this.deleteIfEmpty({ siteID, boardID, threadID: null })
} }
} else if (!Object.keys(this.data[siteID].boards[boardID]).length) { } else if (!Object.keys(this.data[siteID].boards[boardID]).length) {
return delete this.data[siteID].boards[boardID] return delete this.data[siteID].boards[boardID]
@ -201,7 +201,7 @@ export default class DataBoard {
if (postID != null) { if (postID != null) {
for (thread = 0; thread < board.length; thread++) { for (thread = 0; thread < board.length; thread++) {
var ID = board[thread] var ID = board[thread]
if (postID in thread) { if (ID == postID) {
val = thread[postID] val = thread[postID]
break break
} }
@ -221,7 +221,7 @@ export default class DataBoard {
const siteID = g.SITE.ID const siteID = g.SITE.ID
for (boardID in this.data[siteID].boards) { for (boardID in this.data[siteID].boards) {
var val = this.data[siteID].boards[boardID] var val = this.data[siteID].boards[boardID]
this.deleteIfEmpty({ siteID, boardID }) this.deleteIfEmpty({ siteID, boardID, threadID: val })
} }
const now = Date.now() const now = Date.now()
if ( if (
@ -288,7 +288,7 @@ export default class DataBoard {
} }
} }
this.data[siteID].boards[boardID] = threads this.data[siteID].boards[boardID] = threads
this.deleteIfEmpty({ siteID, boardID }) this.deleteIfEmpty({ siteID, boardID, threadID: null })
return $.set(this.key, this.data) return $.set(this.key, this.data)
} }

View File

@ -18,6 +18,19 @@ const $ = (selector, root = document.body) => root.querySelector(selector);
$.id = id => d.getElementById(id); $.id = id => d.getElementById(id);
$.cache = dict(); $.cache = dict();
$.ajaxPage = function (url, options) {
if (options == null) { options = {}; }
const { onloadend, timeout, responseType, withCredentials, type, onprogress, form, headers } = options;
const r = new XMLHttpRequest();
const id = ++Request;
const e = new CustomEvent('4chanXAjax', { detail: { url, timeout, responseType, withCredentials, type, onprogress, form, headers, id } });
d.dispatchEvent(e);
r.onloadend = function () {
delete window.FCX.requests[id];
return onloadend.apply(this, arguments);
};
return r;
}
$.ready = function (fc) { $.ready = function (fc) {
if (d.readyState !== 'loading') { if (d.readyState !== 'loading') {
$.queueTask(fc); $.queueTask(fc);
@ -215,30 +228,41 @@ $.ajax = (function () {
// With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses. // With the `If-Modified-Since` header we only receive the HTTP headers and no body for 304 responses.
// This saves a lot of bandwidth and CPU time for both the users and the servers. // This saves a lot of bandwidth and CPU time for both the users and the servers.
$.lastModified = dict(); $.lastModified = dict();
$.whenModified = function (url, bucket, cb, options = {}) { $.whenModified = function(url, bucket, cb, options = {}) {
let t; const { timeout, ajax = $.ajax } = options;
const { timeout, ajax } = options; let params = [];
const params = []; let lastModifiedTime;
// XXX https://bugs.chromium.org/p/chromium/issues/detail?id=643659
if ($.engine === 'blink') { params.push(`s=${bucket}`); } if ($.engine === 'blink') {
if (url.split('/')[2] === 'a.4cdn.org') { params.push(`t=${Date.now()}`); } params.push(`s=${bucket}`);
const url0 = url;
if (params.length) { url += '?' + params.join('&'); }
const headers = dict();
if ((t = $.lastModified[bucket]?.[url0]) != null) {
headers['If-Modified-Since'] = t;
} }
const r = (ajax || $.ajax)(url, { if (url.split('/')[2] === 'a.4cdn.org') {
params.push(`t=${Date.now()}`);
}
const originalUrl = url;
if (params.length) {
url += '?' + params.join('&');
}
const headers = {};
if ((lastModifiedTime = $.lastModified[bucket]?.[originalUrl]) != null) {
headers['If-Modified-Since'] = lastModifiedTime;
}
return ajax(url, {
onloadend() { onloadend() {
($.lastModified[bucket] || ($.lastModified[bucket] = dict()))[url0] = this.getResponseHeader('Last-Modified'); const lastModifiedHeader = this.getResponseHeader('Last-Modified');
return cb.call(this); $.lastModified[bucket] = $.lastModified[bucket] || {};
$.lastModified[bucket][originalUrl] = lastModifiedHeader;
cb.call(this);
}, },
timeout, timeout,
headers headers
}); });
return r;
}; };
(function () { (function () {
const reqs = dict(); const reqs = dict();
$.cache = function (url, cb, options = {}) { $.cache = function (url, cb, options = {}) {