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; 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 // handle delete command
function nntpchan_delete() { 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 // get the element
var input = document.getElementById("nntpchan_mod_delete"); var input = document.getElementById("nntpchan_mod_target");
// get the long hash // get the long hash
var longhash = get_longhash(input.value); var longhash = get_longhash(input.value);
// TODO: check long hash
var elem = document.getElementById("nntpchan_mod_result"); var elem = document.getElementById("nntpchan_mod_result");
// clear old results // clear old results
while( elem.firstChild ) { while( elem.firstChild ) {
elem.removeChild(elem.firstChild); elem.removeChild(elem.firstChild);
} }
// fire off ajax // fire off ajax
var ajax = new XMLHttpRequest(); var ajax = new XMLHttpRequest();
ajax.onreadystatechange = function() { ajax.onreadystatechange = function() {
@ -46,29 +89,33 @@ function nntpchan_delete() {
var e = document.createTextNode(j.error); var e = document.createTextNode(j.error);
elem.appendChild(e); elem.appendChild(e);
} else { } else {
if ( j.deleted ) { if (mod_action.handle) {
for ( var idx = 0 ; idx < j.deleted.length ; idx ++ ) { var result = mod_action.handle(j);
var deltxt = "deleted " + j.deleted[idx]; if (result) {
var e = document.createTextNode(deltxt); elem.appendChild(result);
elem.appendChild(e); } 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");
} }
} }
} else {
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
} }
input.value = ""; } else {
// nah
// http error
elem.innerHTML = "error: HTTP "+status;
} }
// clear input
input.value = "";
}
if (mod_action.name) {
var url = mod_action.name + "/" + longhash;
ajax.open("GET", url);
ajax.send();
} else {
alert("mod action has no name");
} }
ajax.open("GET", "del/"+longhash);
ajax.send();
} }

View File

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