From 6c723f2eccf85e4bc99782b1550f8c846acb4219 Mon Sep 17 00:00:00 2001 From: flashwave Date: Tue, 17 Dec 2024 21:37:28 +0000 Subject: [PATCH] Made $x use significantly less ugly. --- assets/misuzu.js/embed/embed.js | 2 +- assets/misuzu.js/forum/editor.jsx | 4 +--- assets/misuzu.js/main.js | 8 ++++---- assets/misuzu.js/messages/messages.js | 21 +++++++-------------- assets/misuzu.js/messages/recipient.js | 3 +-- assets/misuzu.js/xhr.js | 13 +++++++------ 6 files changed, 21 insertions(+), 30 deletions(-) diff --git a/assets/misuzu.js/embed/embed.js b/assets/misuzu.js/embed/embed.js index 66e1859..a9403be 100644 --- a/assets/misuzu.js/embed/embed.js +++ b/assets/misuzu.js/embed/embed.js @@ -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); diff --git a/assets/misuzu.js/forum/editor.jsx b/assets/misuzu.js/forum/editor.jsx index c54c5a2..d8f773e 100644 --- a/assets/misuzu.js/forum/editor.jsx +++ b/assets/misuzu.js/forum/editor.jsx @@ -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 = ; diff --git a/assets/misuzu.js/main.js b/assets/misuzu.js/main.js index 883b976..d7913b7 100644 --- a/assets/misuzu.js/main.js +++ b/assets/misuzu.js/main.js @@ -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) { diff --git a/assets/misuzu.js/messages/messages.js b/assets/misuzu.js/messages/messages.js index 74ff985..a9fda88 100644 --- a/assets/misuzu.js/messages/messages.js +++ b/assets/misuzu.js/messages/messages.js @@ -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) diff --git a/assets/misuzu.js/messages/recipient.js b/assets/misuzu.js/messages/recipient.js index ea0ca20..4dcb259 100644 --- a/assets/misuzu.js/messages/recipient.js +++ b/assets/misuzu.js/messages/recipient.js @@ -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); diff --git a/assets/misuzu.js/xhr.js b/assets/misuzu.js/xhr.js index f59d020..c6e4b87 100644 --- a/assets/misuzu.js/xhr.js +++ b/assets/misuzu.js/xhr.js @@ -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; }, }); };