Made $x use significantly less ugly.

This commit is contained in:
Pachira 2024-12-17 21:37:28 +00:00
parent bc2d7a08d0
commit 6c723f2ecc
6 changed files with 21 additions and 30 deletions

View file

@ -74,7 +74,7 @@ const MszEmbed = (function() {
console.error(ex);
})
.then(result => {
const metadata = result.body();
const metadata = result.body;
if(metadata.error) {
replaceWithUrl(targets, url);

View file

@ -195,9 +195,7 @@ const MszForumEditor = function(form) {
formData.append('post[text]', text);
formData.append('post[parser]', parseInt(parser));
const result = await $x.post('/forum/posting.php', { authed: true }, formData);
return result.body();
return (await $x.post('/forum/posting.php', { authed: true }, formData)).body;
};
const previewBtn = <button class="input__button" type="button" value="preview">Preview</button>;

View file

@ -17,11 +17,11 @@
if(!(avatar instanceof Element) || !(userName instanceof Element))
return;
const result = (await $x.get(`/auth/login.php?resolve=1&name=${encodeURIComponent(userName.value)}`, { type: 'json' })).body();
const { body } = await $x.get(`/auth/login.php?resolve=1&name=${encodeURIComponent(userName.value)}`, { type: 'json' });
avatar.src = result.avatar;
if(result.name.length > 0)
userName.value = result.name;
avatar.src = body.avatar;
if(body.name.length > 0)
userName.value = body.name;
};
for(const form of forms) {

View file

@ -40,8 +40,7 @@ const MszMessages = () => {
formData.append('recipient', recipient);
formData.append('reply', replyTo);
const result = await $x.post('/messages/create', { type: 'json', csrf: true }, formData);
const body = result.body();
const { body } = await $x.post('/messages/create', { type: 'json', csrf: true }, formData);
if(body.error !== undefined)
throw body.error;
@ -55,8 +54,7 @@ const MszMessages = () => {
formData.append('parser', parser);
formData.append('draft', draft);
const result = await $x.post(`/messages/${encodeURIComponent(messageId)}`, { type: 'json', csrf: true }, formData);
const body = result.body();
const { body } = await $x.post(`/messages/${encodeURIComponent(messageId)}`, { type: 'json', csrf: true }, formData);
if(body.error !== undefined)
throw body.error;
@ -64,11 +62,10 @@ const MszMessages = () => {
};
const msgsMark = async (msgs, state) => {
const result = await $x.post('/messages/mark', { type: 'json', csrf: true }, {
const { body } = await $x.post('/messages/mark', { type: 'json', csrf: true }, {
type: state,
messages: msgs.map(extractMsgIds).join(','),
});
const body = result.body();
if(body.error !== undefined)
throw body.error;
@ -76,10 +73,9 @@ const MszMessages = () => {
};
const msgsDelete = async msgs => {
const result = await $x.post('/messages/delete', { type: 'json', csrf: true }, {
const { body } = await $x.post('/messages/delete', { type: 'json', csrf: true }, {
messages: msgs.map(extractMsgIds).join(','),
});
const body = result.body();
if(body.error !== undefined)
throw body.error;
@ -87,10 +83,9 @@ const MszMessages = () => {
};
const msgsRestore = async msgs => {
const result = await $x.post('/messages/restore', { type: 'json', csrf: true }, {
const { body } = await $x.post('/messages/restore', { type: 'json', csrf: true }, {
messages: msgs.map(extractMsgIds).join(','),
});
const body = result.body();
if(body.error !== undefined)
throw body.error;
@ -98,11 +93,9 @@ const MszMessages = () => {
};
const msgsNuke = async msgs => {
const result = await $x.post('/messages/nuke', { type: 'json', csrf: true }, {
const { body } = await $x.post('/messages/nuke', { type: 'json', csrf: true }, {
messages: msgs.map(extractMsgIds).join(','),
});
const body = result.body();
if(body.error !== undefined)
throw body.error;
@ -112,7 +105,7 @@ const MszMessages = () => {
const msgsUserBtns = Array.from($qa('.js-header-pms-button'));
if(msgsUserBtns.length > 0)
$x.get('/messages/stats', { type: 'json' }).then(result => {
const body = result.body();
const body = result.body;
if(typeof body === 'object' && typeof body.unread === 'number')
if(body.unread > 0)
for(const msgsUserBtn of msgsUserBtns)

View file

@ -9,10 +9,9 @@ const MszMessagesRecipient = function(element) {
let updateHandler = undefined;
const update = async () => {
const result = await $x.post(element.dataset.msgLookup, { type: 'json', csrf: true }, {
const { body } = await $x.post(element.dataset.msgLookup, { type: 'json', csrf: true }, {
name: nameInput.value,
});
const body = result.body();
if(updateHandler !== undefined)
await updateHandler(body);

View file

@ -84,12 +84,13 @@ const $x = (function() {
MszCSRF.token = headers.get('x-csrf-token');
resolve({
status: xhr.status,
body: () => xhr.response,
text: () => xhr.responseText,
headers: () => headers,
xhr: xhr,
ev: ev,
get ev() { return ev; },
get xhr() { return xhr; },
get status() { return xhr.status; },
get headers() { return headers; },
get body() { return xhr.response; },
get text() { return xhr.responseText; },
});
};