mirror of
https://github.com/LalleSX/4chan-XZ.git
synced 2026-03-20 01:37:47 +01:00
formats and more of dollar
This commit is contained in:
parent
e23dd2366a
commit
f79199366e
@ -97,7 +97,7 @@ $.ajax = (function() {
|
||||
// https://bugs.chromium.org/p/chromium/issues/detail?id=920638
|
||||
$.on(r, 'load', () => {
|
||||
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.
|
||||
if (err.result !== 0x805e0006) { throw err; }
|
||||
r.onloadend = onloadend;
|
||||
$.queueTask($.event, 'error', null, r);
|
||||
$.queueTask($.event, 'loadend', null, r);
|
||||
r.onerror();
|
||||
}
|
||||
return r;
|
||||
});
|
||||
@ -115,8 +114,7 @@ $.ajax = (function() {
|
||||
if (platform === 'userscript') {
|
||||
return r;
|
||||
} else {
|
||||
// # XXX https://bugs.chromium.org/p/chromium/issues/detail?id=920638
|
||||
let requestID = 0;
|
||||
let requestID: number
|
||||
const requests = dict();
|
||||
|
||||
$.ajaxPageInit = function () {
|
||||
@ -124,7 +122,7 @@ $.ajax = (function() {
|
||||
window.FCX.requests = Object.create(null);
|
||||
|
||||
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;
|
||||
window.FCX.requests[id] = (r = new XMLHttpRequest());
|
||||
r.open(type, url, true);
|
||||
@ -137,7 +135,7 @@ $.ajax = (function() {
|
||||
r.timeout = timeout;
|
||||
r.withCredentials = withCredentials;
|
||||
if (onprogress) {
|
||||
r.upload.onprogress = function(e) {
|
||||
r.upload.onprogress = function (e: ProgressEvent) {
|
||||
const { loaded, total } = e;
|
||||
const detail = { loaded, total, id };
|
||||
return document.dispatchEvent(new CustomEvent('4chanXAjaxProgress', { bubbles: true, detail }));
|
||||
@ -156,32 +154,23 @@ $.ajax = (function() {
|
||||
};
|
||||
if (form) {
|
||||
fd = new FormData();
|
||||
for (var entry of form) {
|
||||
fd.append(entry[0], entry[1]);
|
||||
for (var [key, value] of form.entries()) {
|
||||
fd.append(key, value);
|
||||
}
|
||||
} else {
|
||||
fd = null;
|
||||
}
|
||||
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;
|
||||
if (!(req = requests[e.detail.id])) { return; }
|
||||
return req.upload.onprogress.call(req.upload, e.detail);
|
||||
});
|
||||
|
||||
return $.on(d, '4chanXAjaxLoadend', function(e) {
|
||||
let req;
|
||||
return $.on(d, '4chanXAjaxLoadend', function (e: CustomEvent) {
|
||||
let req: XMLHttpRequest;
|
||||
if (!(req = requests[e.detail.id])) { return; }
|
||||
delete requests[e.detail.id];
|
||||
if (e.detail.status) {
|
||||
@ -192,22 +181,8 @@ $.ajax = (function() {
|
||||
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() {
|
||||
// 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: VoidFunction) {
|
||||
if (typeof queueMicrotask === 'function') {
|
||||
return queueMicrotask(fn);
|
||||
} else {
|
||||
return setTimeout(fn, 0);
|
||||
}
|
||||
})();
|
||||
};
|
||||
|
||||
$.global = function (fn, data) {
|
||||
if (doc) {
|
||||
const script = $.el('script',
|
||||
{textContent: `(${fn}).call(document.currentScript.dataset);`});
|
||||
const script = $.el('script', { type: 'application/json' }, { textContent: JSON.stringify(data) });
|
||||
if (data) { $.extend(script.dataset, data); }
|
||||
$.add((d.head || doc), script);
|
||||
$.rm(script);
|
||||
@ -530,7 +488,7 @@ $.global = function(fn, data) {
|
||||
}
|
||||
};
|
||||
|
||||
$.bytesToString = function(size) {
|
||||
$.bytesToString = function (size: number) {
|
||||
let unit = 0; // Bytes
|
||||
while (size >= 1024) {
|
||||
size /= 1024;
|
||||
@ -548,10 +506,9 @@ $.bytesToString = function(size) {
|
||||
return `${size} ${['B', 'KB', 'MB', 'GB'][unit]}`;
|
||||
};
|
||||
|
||||
$.minmax = (value, min, max) => value < min ?
|
||||
$.minmax = (value: number, min: number, max: number) => value < min ?
|
||||
min
|
||||
:
|
||||
value > max ?
|
||||
: value > max ?
|
||||
max
|
||||
:
|
||||
value;
|
||||
@ -794,7 +751,8 @@ if (platform === 'crx') {
|
||||
}
|
||||
return Promise.all((() => {
|
||||
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;
|
||||
})()).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) {
|
||||
for (var key in items) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user