From 95dfe14528663923ca2a88ec928f1d8d9df2402b Mon Sep 17 00:00:00 2001 From: bai Date: Fri, 29 Mar 2019 02:14:43 +0000 Subject: Init --- static/js/mobile.js | 447 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 447 insertions(+) create mode 100644 static/js/mobile.js (limited to 'static/js/mobile.js') diff --git a/static/js/mobile.js b/static/js/mobile.js new file mode 100644 index 0000000..b4dd8cf --- /dev/null +++ b/static/js/mobile.js @@ -0,0 +1,447 @@ +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 = postform.board.value; + 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 = '

' + pad + ' ' + s_name + '

' + s_img + '
' + post.message + '

' + s_time + '|||

'; + else if (post.IS_DELETED == 1) + div.innerHTML = '

' + pad + ' : Eliminado por el usuario.

'; + else + div.innerHTML = '

' + pad + ' : Eliminado por miembro del staff.

'; + } else { + if (post.IS_DELETED == 0) { + div.innerHTML = '

' + s_name + ' ' + s_time + ' ' + post.id + '|||

' + s_img + '
' + post.message + '
'; + } else if (post.IS_DELETED == 1) { div.innerHTML = '

No.' + post.id + ' eliminado por el usuario.

'; } + else { div.innerHTML = '

No.' + post.id + ' eliminado por miembro del staff.

'; } + } + + div.getElementsByClassName("mnu")[0].addEventListener("click", showMenu); + div.getElementsByClassName("num")[0].addEventListener("click", postClick); + thread_div.insertBefore(div, last_elem); + document.getElementsByTagName("h1")[0].getElementsByTagName("span")[0].innerText = "(" + (thread_length + i + 1) + ")" + } + + if (posts.length > 0) { + if (!manual) refreshMaxTime = 10; + if (!document.hasFocus()) + if (posts.length > 1) notif(thread_title, posts.length + ' nuevos mensajes'); + else notif(thread_title, 'Un nuevo mensaje'); + } else { if (refreshMaxTime <= 60) refreshMaxTime += 5; } + + thread_length = parseInt(total_replies) + 1; + new_unread = thread_length - thread_first_length; + + if (new_unread) document.title = '(' + new_unread + ') ' + thread_title; + else document.title = thread_title; +} + +function notif(title, msg) { + var n = new Notification(title, { body: msg }); + setTimeout(n.close.bind(n), 10000); +} + +function counter() { + if (refreshTime < 1) loadJSON(); + else { + refreshTime--; + document.getElementById("counter").innerHTML = (refreshTime + 1); + } +} + +function detectService() { + if (document.getElementById("thread")) { + thread_title = document.getElementsByTagName("h1")[0].innerHTML.split(" \")[0] + " - " + document.title; + thread_length = parseInt(document.getElementsByTagName("h1")[0].getElementsByTagName("span")[0].innerText.slice(1, -1), 10); + thread_first_length = thread_length; + if (document.body.className === "txt") { + serviceType = 1; + replylist = document.getElementsByClassName("pst"); + thread_lastreply = parseInt(replylist[replylist.length - 1].getElementsByClassName("num")[0].innerText); + if (thread_length == thread_lastreply) { + serviceType = 1; + document.getElementById("n2").setAttribute("style", "border-top:1px solid #c6c7c8;border-left:1px solid #c6c7c8;display:inline-block;text-align:center;width:50%;"); + return true; + } else return false; + } else if (document.body.className === "img") { + serviceType = 2; + document.getElementById("n").innerText = "Ver nuevos posts"; + document.getElementById("n2").setAttribute("style", "border-top:1px solid #333;border-left:1px solid #333;display:inline-block;text-align:center;width:50%;"); + replylist = document.getElementsByClassName("first"); + replylist += document.getElementsByClassName("pst"); + return true; + } + } else return false; +} + +function startCounter() { + refreshTime = refreshMaxTime; + counter(); + refreshInterval = setInterval(counter, 1000); +} + +function stopCounter(str) { + clearInterval(refreshInterval); + document.getElementById("counter").innerHTML = str; +} + +function autoRefresh(e) { + chk_snd = document.getElementById("autosound"); + if (document.getElementById("autorefresh").checked) { + if (chk_snd) chk_snd.disabled = false; + Notification.requestPermission(); + lastTime = Math.floor(Date.now() / 1000); + refreshTime = refreshMaxTime; + startCounter(); + } else { + if (chk_snd) document.getElementById("autosound").disabled = true; + stopCounter("OFF"); + } +} + +http_request.onreadystatechange = function() { + if (http_request.readyState == 4) { + var jsonObj = JSON.parse(http_request.responseText); + if (jsonObj.state == "success") { + updateThread(jsonObj.posts, jsonObj.total_replies, jsonObj.time); + lastTime = jsonObj.time; + if (chk.checked) startCounter(); + } + if (manual) { + document.getElementById("n").style.color = "inherit"; + document.getElementById("n").innerText = "Ver nuevos posts"; + } + manual = 0; + } +} + +function sortList(type) { + for(var i=0;i