aboutsummaryrefslogblamecommitdiff
path: root/static/js/weabotxt.js
blob: fec445da7674e00848023b4afeec64614513b141 (plain) (tree)
1
2
3
4
5
6
7
8
9
                                      

                                                
 
 
                               


                                                                             

 


                                                                                          







                                                          




                                              
                

                                                                                   
                                                          

                         
                                                         
              




                                      

                                                                           




                                                         










                                                                         



     

                       





                                                        

                                                         











                                                                            



                        
                                                       

                         

                                                                       

                                                                   
   

 
                         
                                             




                                         




                           
                                                               
 


                                                            
 

                                                       

                                   
                                    
 
                                                                                     
                                            
 





                                                                          
                                                                                  
                     
 

















                                                       
 




                                                                         


                             
                                                                                             


                             
                                                                                                         


                             
                                                                     



                                            

                                                                                               





                                 
                                                                 



                                        




                                                                            


   










                                                           
          

                                          
   
 
                                                      

 
                                                          


                                                            
 
                                    
  


                                                 




                                                           
     
   
 
                                                    


                                                                   
 

                                                      
                                                                                  
                                                           

   
                                           
                                                     
                                          
                                                 
     
                                                                              
   
 





                                                     
     
   
   












                                                







                                                   
         
var style_cookie = "weabot_style_txt";
if (localStorage.hasOwnProperty(style_cookie)) {
  setTheme(localStorage.getItem(style_cookie));
}

function setTheme(styletitle) {
  var css = document.getElementById("css");
  if (css) css.href = "/static/css/txt/" + styletitle.toLowerCase() + ".css";
  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++) {
    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;
  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);
    }
  }
}

// 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();
  }
  return false;
}

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;
  }
}

function postClick(num) {
  var sel = window.getSelection().toString();
  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);

  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 = "";

  preview.removeAttribute("style");
  preview.innerText = "Cargando...";

  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);
}
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;
}
function is_ie() {
  return document.all && !document.opera;
}

function listSort(e) {
  e.preventDefault();
  var s = this.textContent;
  var table = document.getElementById("threads");
  var arr = Array.prototype.slice.call(table.getElementsByTagName("tr"));
  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++) 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);
  
  var hidp = localStorage.getItem("hid-p-"+board);
  var hid = Array();
  if (hidp) hid = hidp.split("!");
  
  if (post.classList.contains("hidden")) {
    post.classList.remove("hidden");
    if (hid.includes(pid)) hid.splice(hid.indexOf(pid), 1);
  } else {
    post.classList.add("hidden");
    if (!hid.includes(pid)) hid.push(pid);
  }

  localStorage.setItem("hid-p-"+board, hid.join("!"));
}

document.addEventListener("DOMContentLoaded", function() {
  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;
      }
    }
  }

  var forms = document.getElementsByTagName("form");
  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) {
    styleSelect.addEventListener("change", function(e) { setTheme(this.value); });
    styleSelect.value = localStorage.getItem(style_cookie);
  }

  if (document.getElementById("threads")) {
    var srt = document.getElementsByClassName("l_s");
    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);
  if (hidp) {
    var hid = hidp.split("!");
    for (var i = 0; i < hid.length; i++) {
      var post = document.getElementById("p"+hid[i]);
      if (post) post.classList.add("hidden");
    }
  }
});

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);

document.addEventListener("dblclick", function(e) {
  if (e.target.closest("h4")) {
    e.preventDefault();
    togglePost(e.target.closest("h4"));
    return;
  }
}, false);