diff --git a/src/classes/DataBoard.js b/src/classes/DataBoard.js index f73be95..40804e7 100644 --- a/src/classes/DataBoard.js +++ b/src/classes/DataBoard.js @@ -115,7 +115,7 @@ export default class DataBoard { return } delete this.data[siteID].boards[boardID][threadID] - return this.deleteIfEmpty({ siteID, boardID }) + return this.deleteIfEmpty({ siteID, boardID, threadID }) } else { return delete this.data[siteID].boards[boardID] } @@ -129,7 +129,7 @@ export default class DataBoard { if (threadID) { if (!Object.keys(this.data[siteID].boards[boardID][threadID]).length) { 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) { return delete this.data[siteID].boards[boardID] @@ -201,7 +201,7 @@ export default class DataBoard { if (postID != null) { for (thread = 0; thread < board.length; thread++) { var ID = board[thread] - if (postID in thread) { + if (ID == postID) { val = thread[postID] break } @@ -221,7 +221,7 @@ export default class DataBoard { const siteID = g.SITE.ID for (boardID in this.data[siteID].boards) { var val = this.data[siteID].boards[boardID] - this.deleteIfEmpty({ siteID, boardID }) + this.deleteIfEmpty({ siteID, boardID, threadID: val }) } const now = Date.now() if ( @@ -288,7 +288,7 @@ export default class DataBoard { } } this.data[siteID].boards[boardID] = threads - this.deleteIfEmpty({ siteID, boardID }) + this.deleteIfEmpty({ siteID, boardID, threadID: null }) return $.set(this.key, this.data) } diff --git a/src/platform/$.js b/src/platform/$.js index 3a926a1..4ce11fa 100644 --- a/src/platform/$.js +++ b/src/platform/$.js @@ -18,6 +18,19 @@ const $ = (selector, root = document.body) => root.querySelector(selector); $.id = id => d.getElementById(id); $.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) { if (d.readyState !== 'loading') { $.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. // This saves a lot of bandwidth and CPU time for both the users and the servers. $.lastModified = dict(); -$.whenModified = function (url, bucket, cb, options = {}) { - let t; - const { timeout, ajax } = options; - const params = []; - // XXX https://bugs.chromium.org/p/chromium/issues/detail?id=643659 - if ($.engine === 'blink') { params.push(`s=${bucket}`); } - if (url.split('/')[2] === 'a.4cdn.org') { params.push(`t=${Date.now()}`); } - const url0 = url; - if (params.length) { url += '?' + params.join('&'); } - const headers = dict(); - if ((t = $.lastModified[bucket]?.[url0]) != null) { - headers['If-Modified-Since'] = t; +$.whenModified = function(url, bucket, cb, options = {}) { + const { timeout, ajax = $.ajax } = options; + let params = []; + let lastModifiedTime; + + if ($.engine === 'blink') { + params.push(`s=${bucket}`); } - 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() { - ($.lastModified[bucket] || ($.lastModified[bucket] = dict()))[url0] = this.getResponseHeader('Last-Modified'); - return cb.call(this); + const lastModifiedHeader = this.getResponseHeader('Last-Modified'); + $.lastModified[bucket] = $.lastModified[bucket] || {}; + $.lastModified[bucket][originalUrl] = lastModifiedHeader; + cb.call(this); }, timeout, headers }); - return r; }; + (function () { const reqs = dict(); $.cache = function (url, cb, options = {}) {