Archived
1
0

revamp mod.js add ban function

This commit is contained in:
jeff 2015-08-07 10:59:57 -04:00
parent 75198f28f2
commit b25f64cec0
2 changed files with 74 additions and 26 deletions

View File

@ -18,20 +18,63 @@ function get_longhash(str) {
return str;
}
// handle ban command
function nntpchan_ban() {
nntpchan_mod({
name: "ban",
handle: function(j) {
if (j.banned) {
return document.createTextNode(j.banned);
}
}
});
}
// handle delete command
function nntpchan_delete() {
nntpchan_mod({
name: "del",
handle: function(j) {
var elem = document.createElement("div");
if (j.deleted) {
for ( var idx = 0 ; idx < j.deleted.length ; idx ++ ) {
var msg = "deleted: " + j.deleted[idx];
var e = document.createTextNode(msg);
var el = document.createElement("div");
el.appendChild(e);
elem.appendChild(el);
}
}
if (j.notdeleted) {
for ( var idx = 0 ; idx < j.notdeleted.length ; idx ++ ) {
var msg = "not deleted: " + j.notdeleted[idx];
var e = document.createTextNode(msg);
var el = document.createElement("div");
el.appendChild(e);
elem.appendChild(el);
}
}
return elem;
}
});
}
function nntpchan_mod(mod_action) {
// get the element
var input = document.getElementById("nntpchan_mod_delete");
var input = document.getElementById("nntpchan_mod_target");
// get the long hash
var longhash = get_longhash(input.value);
// TODO: check long hash
var elem = document.getElementById("nntpchan_mod_result");
// clear old results
while( elem.firstChild ) {
elem.removeChild(elem.firstChild);
}
// fire off ajax
var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() {
@ -46,18 +89,17 @@ function nntpchan_delete() {
var e = document.createTextNode(j.error);
elem.appendChild(e);
} else {
if ( j.deleted ) {
for ( var idx = 0 ; idx < j.deleted.length ; idx ++ ) {
var deltxt = "deleted " + j.deleted[idx];
var e = document.createTextNode(deltxt);
elem.appendChild(e);
if (mod_action.handle) {
var result = mod_action.handle(j);
if (result) {
elem.appendChild(result);
} else {
// fail
alert("mod action failed, handler returned nothing");
}
}
if ( j.notdeleted ) {
for ( var idx = 0 ; idx < j.notdeleted.length ; idx ++ ) {
var deltxt = "failed to delete " + j.notdeleted[idx];
var e = document.createTextNode(deltxt);
elem.appendChild(e);
} else {
// fail
alert("mod action has no handler");
}
}
}
@ -66,9 +108,14 @@ function nntpchan_delete() {
// http error
elem.innerHTML = "error: HTTP "+status;
}
// clear input
input.value = "";
}
}
ajax.open("GET", "del/"+longhash);
if (mod_action.name) {
var url = mod_action.name + "/" + longhash;
ajax.open("GET", url);
ajax.send();
} else {
alert("mod action has no name");
}
}

View File

@ -16,12 +16,13 @@
<div> post actions </div>
<hr />
<div>
<label for="nntpchan_mod_delete">delete by url</label>
<input id="nntpchan_mod_delete" type="text" />
<label for="nntpchan_mod_target">url</label>
<input id="nntpchan_mod_target" type="text" />
</div>
<button id="nntpchan_mod" onclick="nntpchan_delete()">go</button>
<span id="nntpchan_mod_result"></span>
<button onclick="nntpchan_delete()">delete</button>
<button onclick="nntpchan_ban()">ban</button>
</div>
<div id="nntpchan_mod_result"></div>
<noscript>
<b>enable js to use the mod panel kthx</b>
</noscript>