diff options
author | Renard | 2020-03-29 18:47:51 -0300 |
---|---|---|
committer | Renard | 2020-03-29 18:47:51 -0300 |
commit | 18f60364b920e0ff33bc4b8e3135590effeaf173 (patch) | |
tree | c4067d1b5316882c5b8671c22701b963359db227 /static/js/weabotxt.js | |
parent | 3600b2d2fc91b8a15237cff0f27a2ac423ea6c58 (diff) | |
download | weabot-18f60364b920e0ff33bc4b8e3135590effeaf173.tar.gz weabot-18f60364b920e0ff33bc4b8e3135590effeaf173.tar.xz weabot-18f60364b920e0ff33bc4b8e3135590effeaf173.zip |
Prettier para JS
Diffstat (limited to 'static/js/weabotxt.js')
-rw-r--r-- | static/js/weabotxt.js | 353 |
1 files changed, 227 insertions, 126 deletions
diff --git a/static/js/weabotxt.js b/static/js/weabotxt.js index 1301387..ba3e51d 100644 --- a/static/js/weabotxt.js +++ b/static/js/weabotxt.js @@ -1,88 +1,102 @@ var style_cookie = "weabot_style_txt"; -if(style_cookie && localStorage.hasOwnProperty(style_cookie)) { +if (style_cookie && localStorage.hasOwnProperty(style_cookie)) { set_stylesheet(localStorage.getItem(style_cookie)); } var hiddenposts = Array(); function set_stylesheet(styletitle) { - var css=document.getElementById("css"); - if(css) css.href="/static/css/txt/"+styletitle.toLowerCase()+".css"; - localStorage.setItem(style_cookie,styletitle); + var css = document.getElementById("css"); + if (css) css.href = "/static/css/txt/" + styletitle.toLowerCase() + ".css"; + localStorage.setItem(style_cookie, styletitle); } function timeMob(timestamp) { - var time = Math.round(Date.now()/1000); + var time = Math.round(Date.now() / 1000); var el = time - timestamp; - if (el<60) return el + "s"; - else if (el<3600) return Math.round(el/60) + "m"; - else if (el<86400) return Math.round(el/3600) + "h"; - else if (el<2628000) return Math.round(el/86400) + "d"; - else if (el<31536000) return Math.round(el/2628000) + "me"; - else return Math.round(el/31557600) + "a"; + if (el < 60) return el + "s"; + else if (el < 3600) return Math.round(el / 60) + "m"; + else if (el < 86400) return Math.round(el / 3600) + "h"; + else if (el < 2628000) return Math.round(el / 86400) + "d"; + else if (el < 31536000) return Math.round(el / 2628000) + "me"; + else return Math.round(el / 31557600) + "a"; } /* IE/Opera fix, because they need to go learn a book on how to use indexOf with arrays */ if (!Array.prototype.indexOf) { Array.prototype.indexOf = function(elt /*, from*/) { - var len = this.length; - var from = Number(arguments[1]) || 0; - from = (from < 0) ? Math.ceil(from) : Math.floor(from); - if (from < 0) from += len; - for (; from < len; from++) { if (from in this && this[from] === elt) return from; } - return -1; + var len = this.length; + var from = Number(arguments[1]) || 0; + from = from < 0 ? Math.ceil(from) : Math.floor(from); + if (from < 0) from += len; + for (; from < len; from++) { + if (from in this && this[from] === elt) return from; + } + return -1; }; } function getPassword() { if (weabot.password) return weabot.password; - var pass=""; - var char="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_."; - while (pass.length<10) { - pass += char[Math.floor(Math.random()*char.length)]; + var pass = ""; + var char = + "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_."; + while (pass.length < 10) { + pass += char[Math.floor(Math.random() * char.length)]; } weabot.password = pass; localStorage.setItem("weabot", JSON.stringify(weabot)); - return(pass); + 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; + 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(id) { if (document.getElementById(id)) { - with(document.getElementById(id)) { - 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(); - if(typeof preview !== 'undefined') { preview.id = id; preview.addEventListener("click", previewPost); } - addEventListener("submit", saveInputs); + with (document.getElementById(id)) { + 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(); + if (typeof preview !== "undefined") { + preview.id = id; + preview.addEventListener("click", previewPost); + } + addEventListener("submit", saveInputs); } } } // Textboard data 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(); - } + 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; } @@ -90,68 +104,115 @@ function deletePost(e) { var ids = e.parentElement.firstChild.href.split("/"); var post = ids.pop(); var realid = ids.pop(); - if(confirm("¿Seguro que deseas borrar el mensaje "+post+"?")) { - var script="/cgi/delete"; - document.location=script+"?board="+board+"&password="+weabot.password+"&delete="+realid; - } + if (confirm("¿Seguro que deseas borrar el mensaje " + post + "?")) { + var script = "/cgi/delete"; + document.location = + script + + "?board=" + + board + + "&password=" + + weabot.password + + "&delete=" + + realid; + } } function postClick(num) { var sel = window.getSelection().toString(); - if (sel) { sel=sel.replace(/^/gm, ">")+"\n"; sel="\n"+sel; } - insert(">>"+num+sel); + if (sel) { + sel = sel.replace(/^/gm, ">") + "\n"; + sel = "\n" + sel; + } + insert(">>" + num + sel); } function previewPost(e) { var formid = e.target.id; var thread = "0"; - if(formid.startsWith("postform")) thread = formid.substr(8); + if (formid.startsWith("postform")) thread = formid.substr(8); - var form=document.getElementById(formid); - var preview=document.getElementById("preview"+thread); - var main=document.getElementById("options"); + var form = document.getElementById(formid); + var preview = document.getElementById("preview" + thread); + var main = document.getElementById("options"); - if(!form||!preview||!form.message.value) return; - if(main) main.style.display=""; + if (!form || !preview || !form.message.value) return; + if (main) main.style.display = ""; preview.removeAttribute("style"); - preview.innerHTML="<em>Cargando...</em>"; + preview.innerHTML = "<em>Cargando...</em>"; - var text="message="+encodeURIComponent(form.message.value)+"&board="+board; - if (thread) text+="&parentid="+thread; + var text = + "message=" + encodeURIComponent(form.message.value) + "&board=" + board; + if (thread) text += "&parentid=" + thread; - var xmlhttp=get_xmlhttp(); - xmlhttp.open("POST", "/cgi/preview"); - xmlhttp.onreadystatechange=function() { if(xmlhttp.readyState==4) preview.innerHTML=xmlhttp.responseText; } - if(is_ie()||xmlhttp.setRequestHeader) xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); - xmlhttp.send(text); + var xmlhttp = get_xmlhttp(); + xmlhttp.open("POST", "/cgi/preview"); + xmlhttp.onreadystatechange = function() { + if (xmlhttp.readyState == 4) preview.innerHTML = xmlhttp.responseText; + }; + if (is_ie() || xmlhttp.setRequestHeader) + xmlhttp.setRequestHeader( + "Content-Type", + "application/x-www-form-urlencoded" + ); + xmlhttp.send(text); } function listSort() { var s = opcs.sort; var cont = document.getElementById("content"); var elem = document.getElementsByClassName("row"); - var arr = Array.prototype.slice.call(elem); arr.shift(); - if (s=="Normal") { arr.sort(function (a,b) { return (parseInt(a.childNodes[1].textContent) - parseInt(b.childNodes[1].textContent)) }); } - else if(s=="Edad"){ arr.sort(function (a,b) { return (b.children[1].firstChild.href.split("/")[5] - a.children[1].firstChild.href.split("/")[5]) }); } - else if(s=="Largo"){ arr.sort(function (a,b) { return (b.children[2].textContent - a.children[2].textContent) }); } - else if(s=="Rapidez"){ var now=Math.round(Date.now()/1000); arr.sort(function (a,b) { return ((b.children[2].textContent/(now-b.children[1].firstChild.href.split("/")[5])) - (a.children[2].textContent/(now-a.children[1].firstChild.href.split("/")[5]))) }); } - else if(s=="Aleatorio"){ arr.sort(function(a,b) { return 0.5-Math.random()}); } - for (var j=0;j<arr.length;j++) cont.appendChild(arr[j]); + var arr = Array.prototype.slice.call(elem); + arr.shift(); + if (s == "Normal") { + arr.sort(function(a, b) { + return ( + parseInt(a.childNodes[1].textContent) - + parseInt(b.childNodes[1].textContent) + ); + }); + } else if (s == "Edad") { + arr.sort(function(a, b) { + return ( + b.children[1].firstChild.href.split("/")[5] - + a.children[1].firstChild.href.split("/")[5] + ); + }); + } else if (s == "Largo") { + arr.sort(function(a, b) { + return b.children[2].textContent - a.children[2].textContent; + }); + } else if (s == "Rapidez") { + var now = Math.round(Date.now() / 1000); + arr.sort(function(a, b) { + return ( + b.children[2].textContent / + (now - b.children[1].firstChild.href.split("/")[5]) - + a.children[2].textContent / + (now - a.children[1].firstChild.href.split("/")[5]) + ); + }); + } else if (s == "Aleatorio") { + arr.sort(function(a, b) { + return 0.5 - Math.random(); + }); + } + for (var j = 0; j < arr.length; j++) cont.appendChild(arr[j]); localStorage.setItem("threadlist", JSON.stringify(opcs)); } function listDisplay() { var d = opcs.disp; - if (d=="Malla") { + if (d == "Malla") { document.getElementById("header").style.display = "none"; document.getElementById("content").className = "grid"; var style = document.createElement("style"); style.id = "labels"; style.type = "text/css"; - style.textContent = '#content .row div.date{display:none}#content.grid div.com:before{content:"("}#content.grid .com:after{content:")"}'; + style.textContent = + '#content .row div.date{display:none}#content.grid div.com:before{content:"("}#content.grid .com:after{content:")"}'; document.head.appendChild(style); - } else if (d=="Lista") { + } else if (d == "Lista") { var style = document.getElementById("labels"); if (style) style.parentNode.removeChild(style); document.getElementById("header").removeAttribute("style"); @@ -163,24 +224,32 @@ function listDisplay() { function searchSubjects(e) { var filter = this.value.toLowerCase(); var nodes = document.getElementsByClassName("thread"); - for (i=0; i<nodes.length; i++) { - if (nodes[i].textContent.toLowerCase().includes(filter)) nodes[i].parentNode.removeAttribute("style"); + for (i = 0; i < nodes.length; i++) { + if (nodes[i].textContent.toLowerCase().includes(filter)) + nodes[i].parentNode.removeAttribute("style"); else nodes[i].parentNode.style.display = "none"; } } function get_xmlhttp() { - var xmlhttp; - try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); } - catch(e1) { - try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } - catch(e1) { xmlhttp=null; } - } - if(!xmlhttp && typeof XMLHttpRequest!='undefined') xmlhttp=new XMLHttpRequest(); - return(xmlhttp); + var xmlhttp; + try { + xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); + } catch (e1) { + try { + xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); + } catch (e1) { + xmlhttp = null; + } + } + if (!xmlhttp && typeof XMLHttpRequest != "undefined") + xmlhttp = new XMLHttpRequest(); + return xmlhttp; } -function is_ie() { return(document.all&&!document.opera); } +function is_ie() { + return document.all && !document.opera; +} function showpost(post) { post.children[0].classList.remove("hidden"); @@ -194,47 +263,54 @@ function togglepost(e) { e.preventDefault(); var post = this.parentElement; var postid = board + this.getElementsByClassName("date")[0].dataset.unix; - if(post.children[1].style.display == 'none') { + if (post.children[1].style.display == "none") { showpost(post); - if(hiddenposts.includes(postid)) hiddenposts.splice(hiddenposts.indexOf(postid), 1); + if (hiddenposts.includes(postid)) + hiddenposts.splice(hiddenposts.indexOf(postid), 1); } else { hidepost(post); - if(!hiddenposts.includes(postid)) hiddenposts.push(postid); + if (!hiddenposts.includes(postid)) hiddenposts.push(postid); } localStorage.setItem("hiddenposts", hiddenposts.join("!")); } -function applyTheme(){ +function applyTheme() { var newTheme = document.getElementById("styles").value; set_stylesheet(newTheme); } document.addEventListener("DOMContentLoaded", function() { - if (localStorage.hasOwnProperty("weabot")) weabot = JSON.parse(localStorage.getItem("weabot")); - else weabot = {"name":null,"email":null,"password":null}; + if (localStorage.hasOwnProperty("weabot")) + weabot = JSON.parse(localStorage.getItem("weabot")); + else weabot = { name: null, email: null, password: null }; board = document.body.dataset.brd; var head = document.getElementById("main_nav"); if (head) { var b = head.getElementsByTagName("a"); - for(var i=0; i<b.length; i++) { - if (b[i].getAttribute("href") == "/"+board+"/") { b[i].className = "cur_brd"; break; } + for (var i = 0; i < b.length; i++) { + if (b[i].getAttribute("href") == "/" + board + "/") { + b[i].className = "cur_brd"; + break; + } } } var dt = document.getElementsByClassName("date"); - if (screen.width<720) { - if(head) { - var b=head.getElementsByTagName("a"); - var s=document.createElement("select"); - for(var i=1;i<b.length;i++) { + if (screen.width < 720) { + if (head) { + var b = head.getElementsByTagName("a"); + var s = document.createElement("select"); + for (var i = 1; i < b.length; i++) { var o = document.createElement("option"); o.value = b[i].href; o.textContent = b[i].textContent; - if(b[i].className=="cur_brd") o.defaultSelected = true; + if (b[i].className == "cur_brd") o.defaultSelected = true; s.appendChild(o); } - s.addEventListener("change", function(e){ window.location.href=this.value; }); + s.addEventListener("change", function(e) { + window.location.href = this.value; + }); head.innerHTML = '<a href="/" target="_top">Bienvenido a Internet</a> | '; head.appendChild(s); head.removeAttribute("style"); @@ -242,16 +318,19 @@ document.addEventListener("DOMContentLoaded", function() { var head2 = document.getElementById("thread_nav"); if (head2) head2.removeChild(head2.childNodes[1]); } - - for(var i=0;i<dt.length;i++) { + + for (var i = 0; i < dt.length; i++) { t = dt[i].textContent; - if(t.includes("ID:")) var id=t.split(" ")[1]; else id=""; - dt[i].textContent="Hace "+timeMob(dt[i].dataset.unix)+" "+id; + if (t.includes("ID:")) var id = t.split(" ")[1]; + else id = ""; + dt[i].textContent = "Hace " + timeMob(dt[i].dataset.unix) + " " + id; } } var forms = document.getElementsByTagName("form"); - for(var i=0;i<forms.length;i++) { if(forms[i].id.startsWith("postform")) setInputs(forms[i].id); } + for (var i = 0; i < forms.length; i++) { + if (forms[i].id.startsWith("postform")) setInputs(forms[i].id); + } var styleSelect = document.getElementById("styles"); if (styleSelect) { @@ -261,35 +340,57 @@ document.addEventListener("DOMContentLoaded", function() { if (document.body.classList.contains("threads")) { var srt = document.getElementsByClassName("l_s"); - for(var i=0;i<srt.length;i++){ srt[i].addEventListener("click",function(e){ e.preventDefault(); opcs.sort=this.textContent; listSort(); }); } + for (var i = 0; i < srt.length; i++) { + srt[i].addEventListener("click", function(e) { + e.preventDefault(); + opcs.sort = this.textContent; + listSort(); + }); + } var dsp = document.getElementsByClassName("l_d"); - for(var i=0;i<dsp.length;i++){ dsp[i].addEventListener("click",function(e){ e.preventDefault(); opcs.disp=this.textContent; listDisplay(); }); } + for (var i = 0; i < dsp.length; i++) { + dsp[i].addEventListener("click", function(e) { + e.preventDefault(); + opcs.disp = this.textContent; + listDisplay(); + }); + } document.getElementById("l_sr").addEventListener("keyup", searchSubjects); - if (localStorage.hasOwnProperty("threadlist")) { opcs = JSON.parse(localStorage.getItem("threadlist")); listSort(); listDisplay(); } - else { opcs = {"sort":"Normal","disp":"Lista"}; localStorage.setItem("threadlist", JSON.stringify(opcs)); } + if (localStorage.hasOwnProperty("threadlist")) { + opcs = JSON.parse(localStorage.getItem("threadlist")); + listSort(); + listDisplay(); + } else { + opcs = { sort: "Normal", disp: "Lista" }; + localStorage.setItem("threadlist", JSON.stringify(opcs)); + } } - if(localStorage.hasOwnProperty("hiddenposts")) + if (localStorage.hasOwnProperty("hiddenposts")) hiddenposts = localStorage.getItem("hiddenposts").split("!"); - for(var i=0;i<dt.length;i++) { - if(hiddenposts.includes(board+dt[i].dataset.unix)) { + for (var i = 0; i < dt.length; i++) { + if (hiddenposts.includes(board + dt[i].dataset.unix)) { console.log(dt[i].dataset.unix); hidepost(dt[i].parentElement.parentElement); } dt[i].parentElement.addEventListener("dblclick", togglepost); } }); -document.addEventListener("click", function(e) { - var txt = e.target.textContent; - if (e.target.className == "num") { - e.preventDefault(); - postClick(txt); - return; - } - if (txt == "del") { - e.preventDefault(); - deletePost(e.target); - return; - } -},false);
\ No newline at end of file +document.addEventListener( + "click", + function(e) { + var txt = e.target.textContent; + if (e.target.className == "num") { + e.preventDefault(); + postClick(txt); + return; + } + if (txt == "del") { + e.preventDefault(); + deletePost(e.target); + return; + } + }, + false +); |