Archived
1
0

update csrf

This commit is contained in:
Jeff Becker 2016-04-12 19:39:56 -04:00
parent bc239360ea
commit 29b2db8198
No known key found for this signature in database
GPG Key ID: AB950234D6EA286B

View File

@ -143,7 +143,7 @@ function nntpchan_delete() {
} }
function nntpchan_mod(mod_action, result_elem) { function nntpchan_mod(mod_action, result_elem) {
// get the element // get the element
var input = document.getElementById("nntpchan_mod_target"); var input = document.getElementById("nntpchan_mod_target");
var target = null; var target = null;
@ -165,50 +165,59 @@ function nntpchan_mod(mod_action, result_elem) {
elem.removeChild(elem.firstChild); elem.removeChild(elem.firstChild);
} }
var csrf_ajax = new XMLHttpRequest();
// fire off ajax csrf_ajax.onreadystatechange = function() {
var ajax = new XMLHttpRequest(); if (csrf_ajax.readyState == XMLHttpRequest.DONE) {
ajax.onreadystatechange = function() { // get csrf token
if (ajax.readyState == XMLHttpRequest.DONE) { var csrf = csrf_ajax.getResponseHeader("X-CSRF-Token");
var status = ajax.status; // fire off ajax
// we gud? var ajax = new XMLHttpRequest();
if (status == 200) { ajax.onreadystatechange = function() {
// yah if (ajax.readyState == XMLHttpRequest.DONE) {
var txt = ajax.responseText; var status = ajax.status;
var j = JSON.parse(txt); // we gud?
if (j.error) { if (status == 200) {
var e = document.createTextNode(j.error); // yah
elem.appendChild(e); var txt = ajax.responseText;
} else { var j = JSON.parse(txt);
if (mod_action.handle) { if (j.error) {
var result = mod_action.handle(j); var e = document.createTextNode(j.error);
if (result) { elem.appendChild(e);
elem.appendChild(result); } else {
if (mod_action.handle) {
var result = mod_action.handle(j);
if (result) {
elem.appendChild(result);
}
}
} }
} else if (status) {
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
}
// clear input
if (input) {
input.value = "";
} }
} }
} else if (status) {
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
} }
// clear input ajax.setRequestHeader("X-CSRF-Token", csrf);
if (input) { if (mod_action.name) {
input.value = ""; var url = mod_action.name + "/" + target;
ajax.open(mod_action.method || "GET", url);
var data = mod_action.data;
if (data) {
ajax.setRequestHeader("Content-type","text/json");
ajax.send(JSON.stringify(data));
} else {
ajax.send();
}
} else {
alert("mod action has no name");
} }
} }
} }
if (mod_action.name) { csrf_ajax.open("");
var url = mod_action.name + "/" + target; csrf_ajax.send();
ajax.open(mod_action.method || "GET", url);
var data = mod_action.data;
if (data) {
ajax.setRequestHeader("Content-type","text/json");
ajax.send(JSON.stringify(data));
} else {
ajax.send();
}
} else {
alert("mod action has no name");
}
} }