From 0eaed637be988183c2f28225bf456db50a4e247f Mon Sep 17 00:00:00 2001 From: justcool393 Date: Fri, 14 Oct 2022 05:01:11 -0700 Subject: [PATCH] use createXhrWithFormKey in more places --- files/assets/js/award_modal.js | 13 ++--- files/assets/js/ban_modal.js | 13 ++--- .../assets/js/comments+submission_listing.js | 8 +-- files/assets/js/comments_admin.js | 18 ++---- files/assets/js/comments_v.js | 58 +++++++------------ files/assets/js/core.js | 21 +++---- files/assets/js/delete_post_modal.js | 12 ++-- 7 files changed, 52 insertions(+), 91 deletions(-) diff --git a/files/assets/js/award_modal.js b/files/assets/js/award_modal.js index d02008bc1..0100b89e6 100644 --- a/files/assets/js/award_modal.js +++ b/files/assets/js/award_modal.js @@ -88,7 +88,7 @@ function vote(type, id, dir) { } const xhr = createXhrWithFormKey("/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection); - xhr.send(form); + xhr[0].send(xhr[1]); } function pick(kind, canbuy1, canbuy2) { @@ -131,14 +131,11 @@ function buy(mb) { form.append(k, data[k]); } } - - - form.append("formkey", formkey()); - xhr.onload = function() { + xhr[0].onload = function() { let data - try {data = JSON.parse(xhr.response)} + try {data = JSON.parse(xhr[0].response)} catch(e) {console.log(e)} - success = xhr.status >= 200 && xhr.status < 300; + success = xhr[0].status >= 200 && xhr.status < 300; showToast(success, getMessageFromJsonData(success, data), true); if (success) { document.getElementById('giveaward').disabled=false; @@ -148,7 +145,7 @@ function buy(mb) { } }; - xhr.send(form); + xhr[0].send(xhr[1]); } diff --git a/files/assets/js/ban_modal.js b/files/assets/js/ban_modal.js index 53c30cc71..e64878608 100644 --- a/files/assets/js/ban_modal.js +++ b/files/assets/js/ban_modal.js @@ -5,20 +5,15 @@ function banModal(link, id, name) { document.getElementById("banUserButton").onclick = function() { let form = new FormData(document.getElementById("banModalForm")); - form.append("formkey", formkey()); - - const xhr = new XMLHttpRequest(); - xhr.open("POST", `/ban_user/${id}?form`); - xhr.setRequestHeader('xhr', 'xhr'); - - xhr.onload = function() { + const xhr = createXhrWithFormKey(`/ban_user/${id}?form`, "POST", form); + xhr[0].onload = function() { let data try {data = JSON.parse(xhr.response)} catch(e) {console.log(e)} - success = xhr.status >= 200 && xhr.status < 300; + success = xhr[0].status >= 200 && xhr[0].status < 300; showToast(success, getMessageFromJsonData(success, data)); }; - xhr.send(form); + xhr[0].send(xhr[1]); } } \ No newline at end of file diff --git a/files/assets/js/comments+submission_listing.js b/files/assets/js/comments+submission_listing.js index 56ee65f5f..f78baed20 100644 --- a/files/assets/js/comments+submission_listing.js +++ b/files/assets/js/comments+submission_listing.js @@ -73,12 +73,8 @@ document.addEventListener("click", function(){ }); function post(url) { - const xhr = new XMLHttpRequest(); - xhr.open("POST", url); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); - xhr.send(form); + const xhr = createXhrWithFormKey(url); + xhr[0].send(xhr[1]); }; function poll_vote_0(oid, parentid, kind) { diff --git a/files/assets/js/comments_admin.js b/files/assets/js/comments_admin.js index ca889388a..ff4222c1b 100644 --- a/files/assets/js/comments_admin.js +++ b/files/assets/js/comments_admin.js @@ -74,22 +74,14 @@ function approveComment2(post_id,button1,button2) { } function adminMuteUser(userId, muteStatus, buttonId) { - let form = new FormData(); - form.append("formkey", formkey()); - - const xhr = new XMLHttpRequest(); - xhr.open("POST", `/mute_user/${userId}/${muteStatus}`); - xhr.setRequestHeader('xhr', 'xhr'); - - xhr.onload = function() { + const xhr = createXhrWithFormKey(`/mute_user/${userId}/${muteStatus}`); + xhr[0].onload = function() { let data - try {data = JSON.parse(xhr.response)} + try {data = JSON.parse(xhr[0].response)} catch(e) {console.log(e)} - success = xhr.status >= 200 && xhr.status < 300; + success = xhr[0].status >= 200 && xhr[0].status < 300; showToast(success, getMessageFromJsonData(success, data)); }; - - xhr.send(form); - + xhr[0].send(xhr[1]); document.getElementById('mute-user-' + buttonId).classList.add("d-none"); } diff --git a/files/assets/js/comments_v.js b/files/assets/js/comments_v.js index 28c89c8b4..7512730aa 100644 --- a/files/assets/js/comments_v.js +++ b/files/assets/js/comments_v.js @@ -29,23 +29,20 @@ function report_commentModal(id, author) { this.innerHTML='Reporting comment'; this.disabled = true; this.classList.add('disabled'); - const xhr = new XMLHttpRequest(); - xhr.open("POST", '/report/comment/'+id); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); + const form = new FormData(); form.append("reason", reason_comment.value); + const xhr = createXhrWithFormKey("/report/comment/" + id, "POST", form); - xhr.onload = function() { + xhr[0].onload = function() { let data - try {data = JSON.parse(xhr.response)} + try {data = JSON.parse(xhr[0].response)} catch(e) {console.log(e)} - success = xhr.status >= 200 && xhr.status < 300; + success = xhr[0].status >= 200 && xhr[0].status < 300; showToast(success, getMessageFromJsonData(success, data)); }; - xhr.onerror=function(){alert(errortext)}; - xhr.send(form); + xhr[0].onerror=function(){alert(errortext)}; + xhr[0].send(xhr[1]); } }; @@ -95,16 +92,12 @@ function toggleEdit(id){ function delete_commentModal(id) { document.getElementById("deleteCommentButton").onclick = function() { - const xhr = new XMLHttpRequest(); - xhr.open("POST", `/delete/comment/${id}`); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); - xhr.onload = function() { + const xhr = createXhrWithFormKey(`/delete/comment/${id}`); + xhr[0].onload = function() { let data - try {data = JSON.parse(xhr.response)} + try {data = JSON.parse(xhr[0].response)} catch(e) {console.log(e)} - if (xhr.status >= 200 && xhr.status < 300 && data && data['message']) { + if (xhr[0].status >= 200 && xhr[0].status < 300 && data && data['message']) { document.getElementsByClassName(`comment-${id}-only`)[0].classList.add('deleted'); document.getElementById(`delete-${id}`).classList.add('d-none'); document.getElementById(`undelete-${id}`).classList.remove('d-none'); @@ -115,7 +108,7 @@ function delete_commentModal(id) { showToast(false, getMessageFromJsonData(false, data)); } }; - xhr.send(form); + xhr[0].send(xhr[1]); }; } @@ -125,22 +118,18 @@ function post_reply(id){ btn.classList.add('disabled'); const form = new FormData(); - form.append('formkey', formkey()); form.append('parent_id', id); form.append('body', document.getElementById('reply-form-body-'+id).value); - try { for (const e of document.getElementById('file-upload').files) form.append('file', e); } catch(e) {} - - const xhr = new XMLHttpRequest(); - xhr.open("post", "/reply"); - xhr.setRequestHeader('xhr', 'xhr'); - xhr.onload=function(){ + + const xhr = createXhrWithFormKey("/reply", "POST", form); + xhr[0].onload=function(){ let data - try {data = JSON.parse(xhr.response)} + try {data = JSON.parse(xhr[0].response)} catch(e) {console.log(e)} if (data && data["comment"]) { const comments = document.getElementById('replies-of-c_' + id); @@ -164,7 +153,7 @@ function post_reply(id){ btn.classList.remove('disabled'); }, 2000); } - xhr.send(form) + xhr[0].send(xhr[1]); } function comment_edit(id){ @@ -173,8 +162,6 @@ function comment_edit(id){ btn.classList.add('disabled'); const form = new FormData(); - - form.append('formkey', formkey()); form.append('body', document.getElementById('comment-edit-body-'+id).value); try { @@ -182,13 +169,10 @@ function comment_edit(id){ form.append('file', e); } catch(e) {} - - const xhr = new XMLHttpRequest(); - xhr.open("post", "/edit_comment/"+id); - xhr.setRequestHeader('xhr', 'xhr'); - xhr.onload=function(){ + const xhr = createXhrWithFormKey("/reply", "POST", form); + xhr[0].onload=function(){ let data - try {data = JSON.parse(xhr.response)} + try {data = JSON.parse(xhr[0].response)} catch(e) {console.log(e)} if (data && data["comment"]) { commentForm=document.getElementById('comment-text-'+id); @@ -205,7 +189,7 @@ function comment_edit(id){ btn.classList.remove('disabled'); }, 1000); } - xhr.send(form) + xhr[0].send(xhr[1]); } function post_comment(fullname, hide){ diff --git a/files/assets/js/core.js b/files/assets/js/core.js index 51d3a8379..b7ec63a2f 100644 --- a/files/assets/js/core.js +++ b/files/assets/js/core.js @@ -82,17 +82,17 @@ function createXhrWithFormKey(url, method="POST", form=null) { form = new FormData(); } form.append("formkey", formkey()); - return xhr; + return [xhr, form]; // hacky but less stupid than what we were doing before } function postToast(t, url, button1, button2, className, extraActions, extraActionsError) { prePostToastNonShopActions(t, url, button1, button2, className); const xhr = createXhrWithFormKey(url); - xhr.onload = function() { - postToastLoad(xhr, className, extraActions, extraActionsError) + xhr[0].onload = function() { + postToastLoad(xhr[0], className, extraActions, extraActionsError) postPostToastNonShopActions(t, url, button1, button2, className) }; - xhr.send(form); + xhr[0].send(xhr[1]); } /* temporary compatability function. js styling wants us to use thisCase so any new things should use that */ @@ -101,28 +101,29 @@ function post_toast(t, url, button1, button2, classname, extra_actions, extra_ac } function post_toast_callback(url, data, callback) { - const xhr = createXhrWithFormKey(url); + let form = new FormData(); if(typeof data === 'object' && data !== null) { for(let k of Object.keys(data)) { form.append(k, data[k]); } } - xhr.onload = function() { + const xhr = createXhrWithFormKey(url, "POST", form); + xhr[0].onload = function() { let result - if (callback) result = callback(xhr); + if (callback) result = callback(xhr[0]); let message; - let success = xhr.status >= 200 && xhr.status < 300; + let success = xhr[0].status >= 200 && xhr[0].status < 300; if (typeof result == "string") { message = result; } else { - message = getMessageFromJsonData(success, JSON.parse(xhr.response)); + message = getMessageFromJsonData(success, JSON.parse(xhr[0].response)); } let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast oldToast.hide(); showToast(success, message); return success; }; - xhr.send(form); + xhr[0].send(xhr[1]); } if (window.location.pathname != '/submit') diff --git a/files/assets/js/delete_post_modal.js b/files/assets/js/delete_post_modal.js index f8336f50e..8133c1bb3 100644 --- a/files/assets/js/delete_post_modal.js +++ b/files/assets/js/delete_post_modal.js @@ -1,15 +1,11 @@ function delete_postModal(id) { document.getElementById("deletePostButton").onclick = function() { - const xhr = new XMLHttpRequest(); - xhr.open("POST", `/delete_post/${id}`); - xhr.setRequestHeader('xhr', 'xhr'); - const form = new FormData() - form.append("formkey", formkey()); - xhr.onload = function() { + const xhr = createXhrWithFormKey(`/delete_post/${id}`); + xhr[0].onload = function() { let data try {data = JSON.parse(xhr.response)} catch(e) {console.log(e)} - success = xhr.status >= 200 && xhr.status < 300; + success = xhr[0].status >= 200 && xhr[0].status < 300; showToast(success, getMessageFromJsonData(success, data)); if (success && data["message"]) { document.getElementById(`post-${id}`).classList.add('deleted'); @@ -21,6 +17,6 @@ function delete_postModal(id) { showToast(false, getMessageFromJsonData(false, data)); } }; - xhr.send(form); + xhr[0].send(xhr[1]); }; } \ No newline at end of file