forked from rDrama/rDrama
1
0
Fork 0

use createXhrWithFormKey in more places

master
justcool393 2022-10-14 05:01:11 -07:00
parent f22ef3a7e5
commit 0eaed637be
7 changed files with 52 additions and 91 deletions

View File

@ -88,7 +88,7 @@ function vote(type, id, dir) {
} }
const xhr = createXhrWithFormKey("/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection); const xhr = createXhrWithFormKey("/vote/" + type.replace('-mobile','') + "/" + id + "/" + votedirection);
xhr.send(form); xhr[0].send(xhr[1]);
} }
function pick(kind, canbuy1, canbuy2) { function pick(kind, canbuy1, canbuy2) {
@ -131,14 +131,11 @@ function buy(mb) {
form.append(k, data[k]); form.append(k, data[k]);
} }
} }
xhr[0].onload = function() {
form.append("formkey", formkey());
xhr.onload = function() {
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr[0].response)}
catch(e) {console.log(e)} 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); showToast(success, getMessageFromJsonData(success, data), true);
if (success) { if (success) {
document.getElementById('giveaward').disabled=false; document.getElementById('giveaward').disabled=false;
@ -148,7 +145,7 @@ function buy(mb) {
} }
}; };
xhr.send(form); xhr[0].send(xhr[1]);
} }

View File

@ -5,20 +5,15 @@ function banModal(link, id, name) {
document.getElementById("banUserButton").onclick = function() { document.getElementById("banUserButton").onclick = function() {
let form = new FormData(document.getElementById("banModalForm")); let form = new FormData(document.getElementById("banModalForm"));
form.append("formkey", formkey()); const xhr = createXhrWithFormKey(`/ban_user/${id}?form`, "POST", form);
xhr[0].onload = function() {
const xhr = new XMLHttpRequest();
xhr.open("POST", `/ban_user/${id}?form`);
xhr.setRequestHeader('xhr', 'xhr');
xhr.onload = function() {
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr.response)}
catch(e) {console.log(e)} 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)); showToast(success, getMessageFromJsonData(success, data));
}; };
xhr.send(form); xhr[0].send(xhr[1]);
} }
} }

View File

@ -73,12 +73,8 @@ document.addEventListener("click", function(){
}); });
function post(url) { function post(url) {
const xhr = new XMLHttpRequest(); const xhr = createXhrWithFormKey(url);
xhr.open("POST", url); xhr[0].send(xhr[1]);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
xhr.send(form);
}; };
function poll_vote_0(oid, parentid, kind) { function poll_vote_0(oid, parentid, kind) {

View File

@ -74,22 +74,14 @@ function approveComment2(post_id,button1,button2) {
} }
function adminMuteUser(userId, muteStatus, buttonId) { function adminMuteUser(userId, muteStatus, buttonId) {
let form = new FormData(); const xhr = createXhrWithFormKey(`/mute_user/${userId}/${muteStatus}`);
form.append("formkey", formkey()); xhr[0].onload = function() {
const xhr = new XMLHttpRequest();
xhr.open("POST", `/mute_user/${userId}/${muteStatus}`);
xhr.setRequestHeader('xhr', 'xhr');
xhr.onload = function() {
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr[0].response)}
catch(e) {console.log(e)} 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)); showToast(success, getMessageFromJsonData(success, data));
}; };
xhr[0].send(xhr[1]);
xhr.send(form);
document.getElementById('mute-user-' + buttonId).classList.add("d-none"); document.getElementById('mute-user-' + buttonId).classList.add("d-none");
} }

View File

@ -29,23 +29,20 @@ function report_commentModal(id, author) {
this.innerHTML='Reporting comment'; this.innerHTML='Reporting comment';
this.disabled = true; this.disabled = true;
this.classList.add('disabled'); this.classList.add('disabled');
const xhr = new XMLHttpRequest(); const form = new FormData();
xhr.open("POST", '/report/comment/'+id);
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
form.append("reason", reason_comment.value); form.append("reason", reason_comment.value);
const xhr = createXhrWithFormKey("/report/comment/" + id, "POST", form);
xhr.onload = function() { xhr[0].onload = function() {
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr[0].response)}
catch(e) {console.log(e)} 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)); showToast(success, getMessageFromJsonData(success, data));
}; };
xhr.onerror=function(){alert(errortext)}; xhr[0].onerror=function(){alert(errortext)};
xhr.send(form); xhr[0].send(xhr[1]);
} }
}; };
@ -95,16 +92,12 @@ function toggleEdit(id){
function delete_commentModal(id) { function delete_commentModal(id) {
document.getElementById("deleteCommentButton").onclick = function() { document.getElementById("deleteCommentButton").onclick = function() {
const xhr = new XMLHttpRequest(); const xhr = createXhrWithFormKey(`/delete/comment/${id}`);
xhr.open("POST", `/delete/comment/${id}`); xhr[0].onload = function() {
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
xhr.onload = function() {
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr[0].response)}
catch(e) {console.log(e)} 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.getElementsByClassName(`comment-${id}-only`)[0].classList.add('deleted');
document.getElementById(`delete-${id}`).classList.add('d-none'); document.getElementById(`delete-${id}`).classList.add('d-none');
document.getElementById(`undelete-${id}`).classList.remove('d-none'); document.getElementById(`undelete-${id}`).classList.remove('d-none');
@ -115,7 +108,7 @@ function delete_commentModal(id) {
showToast(false, getMessageFromJsonData(false, data)); 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'); btn.classList.add('disabled');
const form = new FormData(); const form = new FormData();
form.append('formkey', formkey());
form.append('parent_id', id); form.append('parent_id', id);
form.append('body', document.getElementById('reply-form-body-'+id).value); form.append('body', document.getElementById('reply-form-body-'+id).value);
try { try {
for (const e of document.getElementById('file-upload').files) for (const e of document.getElementById('file-upload').files)
form.append('file', e); form.append('file', e);
} }
catch(e) {} catch(e) {}
const xhr = new XMLHttpRequest(); const xhr = createXhrWithFormKey("/reply", "POST", form);
xhr.open("post", "/reply"); xhr[0].onload=function(){
xhr.setRequestHeader('xhr', 'xhr');
xhr.onload=function(){
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr[0].response)}
catch(e) {console.log(e)} catch(e) {console.log(e)}
if (data && data["comment"]) { if (data && data["comment"]) {
const comments = document.getElementById('replies-of-c_' + id); const comments = document.getElementById('replies-of-c_' + id);
@ -164,7 +153,7 @@ function post_reply(id){
btn.classList.remove('disabled'); btn.classList.remove('disabled');
}, 2000); }, 2000);
} }
xhr.send(form) xhr[0].send(xhr[1]);
} }
function comment_edit(id){ function comment_edit(id){
@ -173,8 +162,6 @@ function comment_edit(id){
btn.classList.add('disabled'); btn.classList.add('disabled');
const form = new FormData(); const form = new FormData();
form.append('formkey', formkey());
form.append('body', document.getElementById('comment-edit-body-'+id).value); form.append('body', document.getElementById('comment-edit-body-'+id).value);
try { try {
@ -182,13 +169,10 @@ function comment_edit(id){
form.append('file', e); form.append('file', e);
} }
catch(e) {} catch(e) {}
const xhr = createXhrWithFormKey("/reply", "POST", form);
const xhr = new XMLHttpRequest(); xhr[0].onload=function(){
xhr.open("post", "/edit_comment/"+id);
xhr.setRequestHeader('xhr', 'xhr');
xhr.onload=function(){
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr[0].response)}
catch(e) {console.log(e)} catch(e) {console.log(e)}
if (data && data["comment"]) { if (data && data["comment"]) {
commentForm=document.getElementById('comment-text-'+id); commentForm=document.getElementById('comment-text-'+id);
@ -205,7 +189,7 @@ function comment_edit(id){
btn.classList.remove('disabled'); btn.classList.remove('disabled');
}, 1000); }, 1000);
} }
xhr.send(form) xhr[0].send(xhr[1]);
} }
function post_comment(fullname, hide){ function post_comment(fullname, hide){

View File

@ -82,17 +82,17 @@ function createXhrWithFormKey(url, method="POST", form=null) {
form = new FormData(); form = new FormData();
} }
form.append("formkey", formkey()); 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) { function postToast(t, url, button1, button2, className, extraActions, extraActionsError) {
prePostToastNonShopActions(t, url, button1, button2, className); prePostToastNonShopActions(t, url, button1, button2, className);
const xhr = createXhrWithFormKey(url); const xhr = createXhrWithFormKey(url);
xhr.onload = function() { xhr[0].onload = function() {
postToastLoad(xhr, className, extraActions, extraActionsError) postToastLoad(xhr[0], className, extraActions, extraActionsError)
postPostToastNonShopActions(t, url, button1, button2, className) 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 */ /* 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) { function post_toast_callback(url, data, callback) {
const xhr = createXhrWithFormKey(url); let form = new FormData();
if(typeof data === 'object' && data !== null) { if(typeof data === 'object' && data !== null) {
for(let k of Object.keys(data)) { for(let k of Object.keys(data)) {
form.append(k, data[k]); form.append(k, data[k]);
} }
} }
xhr.onload = function() { const xhr = createXhrWithFormKey(url, "POST", form);
xhr[0].onload = function() {
let result let result
if (callback) result = callback(xhr); if (callback) result = callback(xhr[0]);
let message; let message;
let success = xhr.status >= 200 && xhr.status < 300; let success = xhr[0].status >= 200 && xhr[0].status < 300;
if (typeof result == "string") { if (typeof result == "string") {
message = result; message = result;
} else { } 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 let oldToast = bootstrap.Toast.getOrCreateInstance(document.getElementById('toast-post-' + (success ? 'error': 'success'))); // intentionally reversed here: this is the old toast
oldToast.hide(); oldToast.hide();
showToast(success, message); showToast(success, message);
return success; return success;
}; };
xhr.send(form); xhr[0].send(xhr[1]);
} }
if (window.location.pathname != '/submit') if (window.location.pathname != '/submit')

View File

@ -1,15 +1,11 @@
function delete_postModal(id) { function delete_postModal(id) {
document.getElementById("deletePostButton").onclick = function() { document.getElementById("deletePostButton").onclick = function() {
const xhr = new XMLHttpRequest(); const xhr = createXhrWithFormKey(`/delete_post/${id}`);
xhr.open("POST", `/delete_post/${id}`); xhr[0].onload = function() {
xhr.setRequestHeader('xhr', 'xhr');
const form = new FormData()
form.append("formkey", formkey());
xhr.onload = function() {
let data let data
try {data = JSON.parse(xhr.response)} try {data = JSON.parse(xhr.response)}
catch(e) {console.log(e)} 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)); showToast(success, getMessageFromJsonData(success, data));
if (success && data["message"]) { if (success && data["message"]) {
document.getElementById(`post-${id}`).classList.add('deleted'); document.getElementById(`post-${id}`).classList.add('deleted');
@ -21,6 +17,6 @@ function delete_postModal(id) {
showToast(false, getMessageFromJsonData(false, data)); showToast(false, getMessageFromJsonData(false, data));
} }
}; };
xhr.send(form); xhr[0].send(xhr[1]);
}; };
} }