diff options
Diffstat (limited to 'static/js/weabotxt.js')
-rw-r--r-- | static/js/weabotxt.js | 153 |
1 files changed, 79 insertions, 74 deletions
diff --git a/static/js/weabotxt.js b/static/js/weabotxt.js index 7bbddcb..734a5ae 100644 --- a/static/js/weabotxt.js +++ b/static/js/weabotxt.js @@ -9,54 +9,49 @@ function setTheme(styletitle) { localStorage.setItem(style_cookie, styletitle); } -/* 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; - }; -} - function getPassword() { if (weabot.password) return weabot.password; + var pass = ""; - var char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_."; - for (var i=0; i<11; i++) { + var char = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789?!-_.:;"; + for (var i=0; i<=8; i++) { pass += char[Math.floor(Math.random() * char.length)]; } 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; + if (typeof form.fieldb !== "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" && weabot.name) { + fielda.value = weabot.name; } + if (typeof fielda !== "undefined" && weabot.email) { + fieldb.value = weabot.email; + } + if (!password.value) { + password.value = getPassword(); + } + if (typeof preview !== "undefined") { + preview.dataset.formid = id; + preview.addEventListener("click", previewPost); + } + if (typeof message !== "undefined" && document.body.clientWidth < 600) { + message.addEventListener("input", resizeTextbox); + } + addEventListener("submit", saveInputs); } } @@ -64,13 +59,7 @@ function setInputs(id) { 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) { + if (textarea.setSelectionRange) { // Firefox var start = textarea.selectionStart; var end = textarea.selectionEnd; @@ -106,32 +95,51 @@ function postClick(num) { } function previewPost(e) { - var formid = e.target.id; - var thread = "0"; - if (formid.startsWith("postform")) thread = formid.substr(8); + var form = document.getElementById(e.target.dataset.formid); + var thread = e.target.dataset.formid.split('postform')[1]; + var board = form.board.value; + var preview = document.getElementById('preview' + thread); - var form = document.getElementById(formid); - var preview = document.getElementById("preview" + thread); - var main = document.getElementById("options"); + if (e.target.className == '') { // show preview + if (form.message.value.trim() == '') { + return; + } // empty post, nevermind - if (!form || !preview || !form.message.value) return; - if (main) main.style.display = ""; + e.target.className = 'active'; + preview.textContent = 'Cargando...'; - preview.removeAttribute("style"); - preview.innerText = "Cargando..."; + if (!thread) { // new thread + document.getElementById('tr_preview').removeAttribute('style'); + } else { + preview.removeAttribute('style'); + } + } else { // hide it + if (!thread) { // new thread + document.getElementById('tr_preview').style.display = 'none'; + } else { + preview.style.display = 'none'; + } - var text = "message=" + encodeURIComponent(form.message.value) + "&board=" + board; - if (thread) text += "&parentid=" + thread; + e.target.removeAttribute('class'); + preview.textContent = null; + return; + } + + var post = 'message=' + encodeURIComponent(form.message.value) + '&board=' + board + '&parentid=' + thread; var xmlhttp = get_xmlhttp(); - xmlhttp.open("POST", "/cgi/preview"); + xmlhttp.open('POST', '/cgi/preview'); xmlhttp.onreadystatechange = function() { - if (xmlhttp.readyState == 4) preview.innerHTML = xmlhttp.responseText; + 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); + if (xmlhttp.setRequestHeader) { + xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + } + xmlhttp.send(post); } + function get_xmlhttp() { var xmlhttp; try { @@ -143,13 +151,11 @@ function get_xmlhttp() { xmlhttp = null; } } - if (!xmlhttp && typeof XMLHttpRequest != "undefined") + if (!xmlhttp && typeof XMLHttpRequest != "undefined") { xmlhttp = new XMLHttpRequest(); + } return xmlhttp; } -function is_ie() { - return document.all && !document.opera; -} function listSort(e) { e.preventDefault(); @@ -183,16 +189,6 @@ function listSort(e) { for (var j = 0; j < arr.length; j++) table.appendChild(arr[j]); } -function searchSubjects(e) { - var filter = this.value.toLowerCase(); - var nodes = document.getElementById("threads").getElementsByTagName("tr"); - for (var i = 1; i < nodes.length; i++) { - if (nodes[i].childNodes[3].textContent.toLowerCase().includes(filter)) - nodes[i].removeAttribute("style"); - else nodes[i].style.display = "none"; - } -} - function togglePost(e) { var post = e.parentElement; var pid = post.id.slice(1); @@ -212,10 +208,18 @@ function togglePost(e) { localStorage.setItem("hid-p-"+board, hid.join("!")); } +function resizeTextbox(e) { + var limit = 256; + e.target.style.height = ''; + e.target.style.height = Math.min((e.target.scrollHeight+2), limit) + 'px'; +} + document.addEventListener("DOMContentLoaded", function() { - if (localStorage.hasOwnProperty("weabot")) + if (localStorage.hasOwnProperty("weabot")) { weabot = JSON.parse(localStorage.getItem("weabot")); - else weabot = { name: null, email: null, password: null }; + } else { + weabot = { name: null, email: null, password: null }; + } board = document.body.dataset.brd; @@ -263,7 +267,9 @@ document.addEventListener("DOMContentLoaded", function() { var forms = document.getElementsByTagName("form"); for (var i = 0; i < forms.length; i++) { - if (forms[i].id.startsWith("postform")) setInputs(forms[i].id); + if (forms[i].id.startsWith("postform")) { + setInputs(forms[i].id); + } } var styleSelect = document.getElementById("styles"); @@ -277,7 +283,6 @@ document.addEventListener("DOMContentLoaded", function() { for (var i = 0; i < srt.length; i++) { srt[i].addEventListener("click", listSort); } - document.getElementById("l_sr").addEventListener("keyup", searchSubjects); } var hidp = localStorage.getItem("hid-p-"+board); @@ -292,11 +297,11 @@ document.addEventListener("DOMContentLoaded", function() { document.addEventListener("click", function(e) { var txt = e.target.textContent; - if (e.target.className == "num") { + /*if (e.target.className == "num") { e.preventDefault(); postClick(txt); return; - } + }*/ if (txt == "del") { e.preventDefault(); deletePost(e.target); @@ -311,4 +316,4 @@ document.addEventListener("dblclick", function(e) { window.getSelection().removeAllRanges(); return; } -}, false);
\ No newline at end of file +}, false); |