function sendPost(e) {
e.preventDefault();
var button = document.getElementById("post");
button.disabled = true;
var sendpost = new XMLHttpRequest();
var postform = document.getElementById("postform");
sendpost.open("POST", "/cgi/api/post", true);
sendpost.send(new FormData(postform));
sendpost.onreadystatechange = function() {
if (sendpost.readyState == 4) {
button.disabled = false;
var response = JSON.parse(sendpost.responseText);
if (response.state == "success") {
postform.message.value = "";
checkNew(e);
} else alert(response.message);
}
};
}
function postClick(e) {
e.preventDefault();
var sel = window.getSelection().toString();
if (sel) {
sel = sel.replace(/^/gm, ">") + "\n";
sel = "\n" + sel;
}
insert(">>" + parseInt(this.innerHTML, 10) + sel);
}
function insert(text) {
var textarea = document.forms.postform.message;
if (textarea) {
if (textarea.createTextRange && textarea.caretPos) {
// IE
var caretPos = textarea.caretPos;
caretPos.text =
caretPos.text.charAt(caretPos.text.length - 1) == " "
? text + " "
: text;
} else if (textarea.setSelectionRange) {
// Firefox
var start = textarea.selectionStart;
var end = textarea.selectionEnd;
textarea.value =
textarea.value.substr(0, start) + text + textarea.value.substr(end);
textarea.setSelectionRange(start + text.length, start + text.length);
} else {
textarea.value += text + " ";
}
textarea.focus();
}
return false;
}
function getPassword() {
if (weabot.password) return weabot.password;
var char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
var pass = "";
for (var i = 0; i < 8; i++) {
var rnd = Math.floor(Math.random() * char.length);
pass += char.substring(rnd, rnd + 1);
}
console.log(weabot.password);
weabot.password = pass;
localStorage.setItem("weabot", JSON.stringify(weabot));
return pass;
}
function saveInputs(e) {
var e = e || window.event;
var form = e.target || e.srcElement;
if (typeof form.fielda !== "undefined") weabot.name = form.fielda.value;
if (typeof form.fielda !== "undefined") weabot.email = form.fieldb.value;
localStorage.setItem("weabot", JSON.stringify(weabot));
}
function setInputs() {
with (document.getElementById("postform")) {
if (typeof fielda !== "undefined" && !fielda.value && weabot.name)
fielda.value = weabot.name;
if (typeof fielda !== "undefined" && !fieldb.value && weabot.email)
fieldb.value = weabot.email;
if (!password.value) password.value = getPassword();
addEventListener("submit", saveInputs);
}
}
function showMenu(e) {
e.preventDefault();
if (document.getElementById("mnu-opened")) closeMenu(e);
this.id = "mnu-opened";
var brd = document.body.dataset.brd;
var post = this.parentNode.parentNode;
if (document.body.className === "txt") {
var id = post.id.substr(1);
var num = parseInt(post.getElementsByClassName("num")[0].innerText, 10);
} else {
var id = post.getElementsByClassName("num")[0].innerText;
var num = post.className === "first" ? 1 : 0;
}
var menu = document.createElement("div");
menu.id = "mnu-list";
menu.style.top = e.pageY + 5 + "px";
menu.style.left = e.pageX + 5 + "px";
document.body.appendChild(menu);
menu = document.getElementById("mnu-list");
var rep = document.createElement("a");
rep.href = "#";
rep.innerText = "Denunciar post";
rep.addEventListener("click", function(e) {
var reason = prompt("Razón de denuncia:");
if (reason === "")
while (reason === "") reason = prompt("Error: Ingresa una razón.");
if (reason) {
var rep_req = new XMLHttpRequest();
var report =
"/cgi/report/" + brd +
"/" + id + (num ? "/" + num : "") +
"?reason=" + reason;
rep_req.open("GET", report, true);
rep_req.send();
rep_req.onreadystatechange = function() {
if (rep_req.readyState == 4 && rep_req.status == 200)
alert("Denuncia enviada.");
};
}
});
menu.appendChild(rep);
var del = document.createElement("a");
del.href = "#";
del.innerText = "Eliminar post";
del.addEventListener("click", function(e) {
if (
confirm("¿Seguro que deseas borrar el mensaje " + (num ? num : id) + "?")
) {
var del_req = new XMLHttpRequest();
var del_form =
"/cgi/api/delete?dir=" + brd +
"&id=" + id +
"&password=" + postform.password.value;
del_req.open("GET", del_form, true);
del_req.send();
del_req.onreadystatechange = function() {
if (del_req.readyState == 4) {
var response = JSON.parse(del_req.responseText);
if (response.state == "success") {
if (num == 1) {
alert("Hilo eliminado.");
document.location = "/cgi/mobile/" + brd;
} else {
alert("Mensaje eliminado.");
location.reload();
}
} else if (response.state == "failed") alert(response.message);
}
};
}
});
menu.appendChild(del);
var file = post.getElementsByClassName("thm")[0];
if (file) {
var dfile = document.createElement("a");
dfile.href = "#";
dfile.innerText = "Eliminar archivo";
dfile.addEventListener("click", function(e) {
if (
confirm(
"¿Seguro que deseas borrar el archivo del mensaje " +
(num ? num : id) +
"?"
)
) {
var fdel_req = new XMLHttpRequest();
var fdel_form =
"/cgi/api/delete?dir=" +
brd +
"&id=" +
id +
"&password=" +
postform.password.value +
"&imageonly=true";
fdel_req.open("GET", fdel_form, true);
fdel_req.send();
fdel_req.onreadystatechange = function() {
if (fdel_req.readyState == 4) {
var response = JSON.parse(fdel_req.responseText);
if (response.state == "success") {
alert("Archivo eliminado.");
post.removeChild(file);
} else if (response.state == "failed") alert(response.message);
}
};
}
});
menu.appendChild(dfile);
}
e.stopPropagation();
this.removeEventListener("click", showMenu);
document.addEventListener("click", closeMenu);
}
function closeMenu(e) {
var menu = document.getElementById("mnu-list");
menu.parentElement.removeChild(menu);
document.removeEventListener("click", closeMenu);
var btn = document.getElementById("mnu-opened");
btn.addEventListener("click", showMenu);
btn.removeAttribute("id");
e.preventDefault();
}
function searchSubjects() {
var filter = document.getElementById("search").value.toLowerCase();
var nodes = document
.getElementsByClassName("list")[0]
.getElementsByTagName("a");
for (i = 0; i < nodes.length; i++) {
if (
nodes[i].innerHTML
.toLowerCase()
.split(/<\/?br[^>]*>\s*/im)[0]
.includes(filter)
)
nodes[i].removeAttribute("style");
else nodes[i].style.display = "none";
}
}
function searchCatalog() {
var filter = document.getElementById("catsearch").value.toLowerCase();
var nodes = document.getElementsByClassName("cat");
for (i = 0; i < nodes.length; i++) {
if (
nodes[i].innerText
.toLowerCase()
.substring(nodes[i].innerText.indexOf("R)") + 2)
.includes(filter)
)
nodes[i].removeAttribute("style");
else nodes[i].style.display = "none";
}
}
var lastTime = 0;
var refreshInterval;
var refreshMaxTime = 30;
var refreshTime;
var manual = 0;
var serviceType = 0;
var thread_length = 0;
var thread_lastreply = 0;
var thread_title = "";
var thread_first_length = 0;
var http_request = new XMLHttpRequest();
function checkNew(e) {
e.preventDefault();
manual = 1;
loadJSON();
if (chk.checked) refreshMaxTime = 25;
}
function loadJSON() {
if (chk.checked) stopCounter("...");
if (manual) {
document.getElementById("n").style.color = "gray";
document.getElementById("n").innerText = "Revisando...";
}
var data_file;
if (serviceType)
data_file =
"/cgi/api/thread?dir=" +
postform.board.value +
"&id=" +
postform.parent.value +
"&offset=" +
thread_length +
"&time=" +
lastTime;
else return false;
http_request.open("GET", data_file, true);
http_request.send();
}
function updateThread(posts, total_replies, serverTime) {
thread_div = document.getElementById("thread");
last_elem = document.getElementById("n");
for (var i = 0; i < posts.length; i++) {
post = posts[i];
brd = postform.board.value;
var div = document.createElement("div");
div.className = "pst";
div.id = "p" + post.id;
if (post.IS_DELETED == 0) {
s_name = post.name;
if (post.tripcode) s_name += " " + post.tripcode;
s_time = post.timestamp_formatted.replace(/\(.{1,3}\)/g, " ");
if (post.file)
s_img =
'
' +
Math.round(post.file_size / 1024) +
"KB " +
post.file
.substring(post.file.lastIndexOf(".") + 1, post.file.length)
.toUpperCase() +
"";
else s_img = "";
}
if (serviceType == 1) {
var pad = "0000" + (thread_length + i + 1);
pad = pad.substr(pad.length - 4);
if (post.IS_DELETED == 0)
div.innerHTML =
'