dollar back to js

This commit is contained in:
Lalle 2023-04-18 03:31:32 +02:00
parent f79199366e
commit 04b144ff47
No known key found for this signature in database
GPG Key ID: A6583D207A8F6B0D

View File

@ -14,7 +14,7 @@ import CrossOrigin from "./CrossOrigin";
import { debounce, dict, MINUTE, platform, SECOND } from "./helpers"; import { debounce, dict, MINUTE, platform, SECOND } from "./helpers";
// not chainable // not chainable
const $ = (selector: string, root: HTMLElement = doc) => root.querySelector(selector); const $ = (selector, root = document.body) => root.querySelector(selector);
$.id = id => d.getElementById(id); $.id = id => d.getElementById(id);
@ -97,7 +97,7 @@ $.ajax = (function () {
// https://bugs.chromium.org/p/chromium/issues/detail?id=920638 // https://bugs.chromium.org/p/chromium/issues/detail?id=920638
$.on(r, 'load', () => { $.on(r, 'load', () => {
if (!Conf['Work around CORB Bug'] && r.readyState === 4 && r.status === 200 && r.statusText === '' && r.response === null) { if (!Conf['Work around CORB Bug'] && r.readyState === 4 && r.status === 200 && r.statusText === '' && r.response === null) {
$.set('Work around CORB Bug', (Conf['Work around CORB Bug'] = Date.now()), cb => cb()); $.set('Work around CORB Bug', (Conf['Work around CORB Bug'] = Date.now()));
} }
}); });
} }
@ -106,7 +106,8 @@ $.ajax = (function () {
// XXX Some content blockers in Firefox (e.g. Adblock Plus and NoScript) throw an exception instead of simulating a connection error. // XXX Some content blockers in Firefox (e.g. Adblock Plus and NoScript) throw an exception instead of simulating a connection error.
if (err.result !== 0x805e0006) { throw err; } if (err.result !== 0x805e0006) { throw err; }
r.onloadend = onloadend; r.onloadend = onloadend;
r.onerror(); $.queueTask($.event, 'error', null, r);
$.queueTask($.event, 'loadend', null, r);
} }
return r; return r;
}); });
@ -114,7 +115,8 @@ $.ajax = (function () {
if (platform === 'userscript') { if (platform === 'userscript') {
return r; return r;
} else { } else {
let requestID: number // # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638
let requestID = 0;
const requests = dict(); const requests = dict();
$.ajaxPageInit = function () { $.ajaxPageInit = function () {
@ -122,7 +124,7 @@ $.ajax = (function () {
window.FCX.requests = Object.create(null); window.FCX.requests = Object.create(null);
document.addEventListener('4chanXAjax', function (e) { document.addEventListener('4chanXAjax', function (e) {
let fd: FormData, r: XMLHttpRequest; let fd, r;
const { url, timeout, responseType, withCredentials, type, onprogress, form, headers, id } = e.detail; const { url, timeout, responseType, withCredentials, type, onprogress, form, headers, id } = e.detail;
window.FCX.requests[id] = (r = new XMLHttpRequest()); window.FCX.requests[id] = (r = new XMLHttpRequest());
r.open(type, url, true); r.open(type, url, true);
@ -135,7 +137,7 @@ $.ajax = (function () {
r.timeout = timeout; r.timeout = timeout;
r.withCredentials = withCredentials; r.withCredentials = withCredentials;
if (onprogress) { if (onprogress) {
r.upload.onprogress = function (e: ProgressEvent) { r.upload.onprogress = function (e) {
const { loaded, total } = e; const { loaded, total } = e;
const detail = { loaded, total, id }; const detail = { loaded, total, id };
return document.dispatchEvent(new CustomEvent('4chanXAjaxProgress', { bubbles: true, detail })); return document.dispatchEvent(new CustomEvent('4chanXAjaxProgress', { bubbles: true, detail }));
@ -154,35 +156,58 @@ $.ajax = (function () {
}; };
if (form) { if (form) {
fd = new FormData(); fd = new FormData();
for (var [key, value] of form.entries()) { for (var entry of form) {
fd.append(key, value); fd.append(entry[0], entry[1]);
} }
} else {
fd = null;
} }
return r.send(fd); return r.send(fd);
}); }
}, 'ajaxPageInit'); , false);
return document.addEventListener('4chanXAjaxAbort', function (e) {
let r;
if (!(r = window.FCX.requests[e.detail.id])) { return; }
return r.abort();
}
, false);
});
$.on(d, '4chanXAjaxProgress', function (e) {
let req;
if (!(req = requests[e.detail.id])) { return; }
return req.upload.onprogress.call(req.upload, e.detail);
});
return $.on(d, '4chanXAjaxLoadend', function (e) {
let req;
if (!(req = requests[e.detail.id])) { return; }
delete requests[e.detail.id];
if (e.detail.status) {
for (var key of ['status', 'statusText', 'response', 'responseHeaderString']) {
req[key] = e.detail[key];
}
if (req.responseType === 'document') {
req.response = new DOMParser().parseFromString(e.detail.response, 'text/html');
}
}
return req.onloadend();
});
}; };
$.on(d, '4chanXAjaxProgress', function (e: CustomEvent) { return $.ajaxPage = function (url, options = {}) {
let req; let req;
if (!(req = requests[e.detail.id])) { return; } let { onloadend, timeout, responseType, withCredentials, type, onprogress, form, headers } = options;
return req.upload.onprogress.call(req.upload, e.detail); const id = requestID++;
}); requests[id] = (req = new CrossOrigin.Request());
$.extend(req, { responseType, onloadend });
return $.on(d, '4chanXAjaxLoadend', function (e: CustomEvent) { req.upload = { onprogress };
let req: XMLHttpRequest; req.abort = () => $.event('4chanXAjaxAbort', { id });
if (!(req = requests[e.detail.id])) { return; } if (form) { form = Array.from(form.entries()); }
delete requests[e.detail.id]; $.event('4chanXAjax', { url, timeout, responseType, withCredentials, type, onprogress: !!onprogress, form, headers, id });
if (e.detail.status) { return req;
for (var key of ['status', 'statusText', 'response', 'responseHeaderString']) { };
req[key] = e.detail[key];
}
if (req.responseType === 'document') {
req.response = new DOMParser().parseFromString(e.detail.response, 'text/html');
}
}
return req.onloadend.call(req);
});
} }
})(); })();
@ -464,17 +489,34 @@ $.debounce = function (wait, fn) {
}; };
}; };
$.queueTask = function (fn: VoidFunction) { $.queueTask = (function () {
if (typeof queueMicrotask === 'function') { // inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007
return queueMicrotask(fn); const taskQueue = [];
} else { const execTask = function () {
return setTimeout(fn, 0); 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);
};
} }
}; })();
$.global = function (fn, data) { $.global = function (fn, data) {
if (doc) { if (doc) {
const script = $.el('script', { type: 'application/json' }, { textContent: JSON.stringify(data) }); const script = $.el('script',
{ textContent: `(${fn}).call(document.currentScript.dataset);` });
if (data) { $.extend(script.dataset, data); } if (data) { $.extend(script.dataset, data); }
$.add((d.head || doc), script); $.add((d.head || doc), script);
$.rm(script); $.rm(script);
@ -488,7 +530,7 @@ $.global = function (fn, data) {
} }
}; };
$.bytesToString = function (size: number) { $.bytesToString = function (size) {
let unit = 0; // Bytes let unit = 0; // Bytes
while (size >= 1024) { while (size >= 1024) {
size /= 1024; size /= 1024;
@ -506,9 +548,10 @@ $.bytesToString = function (size: number) {
return `${size} ${['B', 'KB', 'MB', 'GB'][unit]}`; return `${size} ${['B', 'KB', 'MB', 'GB'][unit]}`;
}; };
$.minmax = (value: number, min: number, max: number) => value < min ? $.minmax = (value, min, max) => value < min ?
min min
: value > max ? :
value > max ?
max max
: :
value; value;
@ -906,7 +949,7 @@ if (platform === 'crx') {
} }
}; };
$.get = $.oneItemSugar((items, cb) => $.queueTask(() => $.getSync(items, cb))); $.get = $.oneItemSugar((items, cb) => $.queueTask($.getSync, items, cb));
$.getSync = function (items, cb) { $.getSync = function (items, cb) {
for (var key in items) { for (var key in items) {
@ -949,4 +992,4 @@ if (platform === 'crx') {
} }
} }
export default $; export default $;