formats and more of dollar

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

View File

@ -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())); $.set('Work around CORB Bug', (Conf['Work around CORB Bug'] = Date.now()), cb => cb());
} }
}); });
} }
@ -106,8 +106,7 @@ $.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;
$.queueTask($.event, 'error', null, r); r.onerror();
$.queueTask($.event, 'loadend', null, r);
} }
return r; return r;
}); });
@ -115,8 +114,7 @@ $.ajax = (function() {
if (platform === 'userscript') { if (platform === 'userscript') {
return r; return r;
} else { } else {
// # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638 let requestID: number
let requestID = 0;
const requests = dict(); const requests = dict();
$.ajaxPageInit = function () { $.ajaxPageInit = function () {
@ -124,7 +122,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, r; let fd: FormData, r: XMLHttpRequest;
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);
@ -137,7 +135,7 @@ $.ajax = (function() {
r.timeout = timeout; r.timeout = timeout;
r.withCredentials = withCredentials; r.withCredentials = withCredentials;
if (onprogress) { if (onprogress) {
r.upload.onprogress = function(e) { r.upload.onprogress = function (e: ProgressEvent) {
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 }));
@ -156,32 +154,23 @@ $.ajax = (function() {
}; };
if (form) { if (form) {
fd = new FormData(); fd = new FormData();
for (var entry of form) { for (var [key, value] of form.entries()) {
fd.append(entry[0], entry[1]); fd.append(key, value);
} }
} else {
fd = null;
} }
return r.send(fd); return r.send(fd);
}
, false);
return document.addEventListener('4chanXAjaxAbort', function(e) {
let r;
if (!(r = window.FCX.requests[e.detail.id])) { return; }
return r.abort();
}
, false);
}); });
}, 'ajaxPageInit');
};
$.on(d, '4chanXAjaxProgress', function(e) { $.on(d, '4chanXAjaxProgress', function (e: CustomEvent) {
let req; let req;
if (!(req = requests[e.detail.id])) { return; } if (!(req = requests[e.detail.id])) { return; }
return req.upload.onprogress.call(req.upload, e.detail); return req.upload.onprogress.call(req.upload, e.detail);
}); });
return $.on(d, '4chanXAjaxLoadend', function(e) { return $.on(d, '4chanXAjaxLoadend', function (e: CustomEvent) {
let req; let req: XMLHttpRequest;
if (!(req = requests[e.detail.id])) { return; } if (!(req = requests[e.detail.id])) { return; }
delete requests[e.detail.id]; delete requests[e.detail.id];
if (e.detail.status) { if (e.detail.status) {
@ -192,22 +181,8 @@ $.ajax = (function() {
req.response = new DOMParser().parseFromString(e.detail.response, 'text/html'); req.response = new DOMParser().parseFromString(e.detail.response, 'text/html');
} }
} }
return req.onloadend(); return req.onloadend.call(req);
}); });
};
return $.ajaxPage = function(url, options={}) {
let req;
let {onloadend, timeout, responseType, withCredentials, type, onprogress, form, headers} = options;
const id = requestID++;
requests[id] = (req = new CrossOrigin.Request());
$.extend(req, {responseType, onloadend});
req.upload = {onprogress};
req.abort = () => $.event('4chanXAjaxAbort', {id});
if (form) { form = Array.from(form.entries()); }
$.event('4chanXAjax', {url, timeout, responseType, withCredentials, type, onprogress: !!onprogress, form, headers, id});
return req;
};
} }
})(); })();
@ -489,34 +464,17 @@ $.debounce = function(wait, fn) {
}; };
}; };
$.queueTask = (function() { $.queueTask = function (fn: VoidFunction) {
// inspired by https://www.w3.org/Bugs/Public/show_bug.cgi?id=15007 if (typeof queueMicrotask === 'function') {
const taskQueue = []; return queueMicrotask(fn);
const execTask = function() { } else {
const task = taskQueue.shift(); return setTimeout(fn, 0);
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', const script = $.el('script', { type: 'application/json' }, { textContent: JSON.stringify(data) });
{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);
@ -530,7 +488,7 @@ $.global = function(fn, data) {
} }
}; };
$.bytesToString = function(size) { $.bytesToString = function (size: number) {
let unit = 0; // Bytes let unit = 0; // Bytes
while (size >= 1024) { while (size >= 1024) {
size /= 1024; size /= 1024;
@ -548,10 +506,9 @@ $.bytesToString = function(size) {
return `${size} ${['B', 'KB', 'MB', 'GB'][unit]}`; return `${size} ${['B', 'KB', 'MB', 'GB'][unit]}`;
}; };
$.minmax = (value, min, max) => value < min ? $.minmax = (value: number, min: number, max: number) => value < min ?
min min
: : value > max ?
value > max ?
max max
: :
value; value;
@ -794,7 +751,8 @@ if (platform === 'crx') {
} }
return Promise.all((() => { return Promise.all((() => {
const result = []; const result = [];
for (key of keys) { result.push(GM.deleteValue(g.NAMESPACE + key)); for (key of keys) {
result.push(GM.deleteValue(g.NAMESPACE + key));
} }
return result; return result;
})()).then(function () { })()).then(function () {
@ -948,7 +906,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) {