aboutsummaryrefslogtreecommitdiff
path: root/static/js/weabotxt.js
blob: 9cd407c2d8f3577eab76381ed5005a2b2ef89721 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
var style_cookie = "weabot_style_txt";
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);
}

function changeDate() {
  var dts = document.getElementsByClassName("date");
  if (dts[0].dataset.unix) {
    week = ["dom", "lun", "mar", "mie", "jue", "vie", "sab"];
    if (board == "world") week = ["sun", "mon", "tue", "wed", "thu", "fri", "sat"];
    for(var d=0;d<dts.length;d++) {
      dts[d].addEventListener('mouseover', function(e) { this.title = "Hace " + timeAgo(this.dataset.unix); });
      if (dts[d].innerText.includes("ID:")) var id = dts[d].innerText.split(" ")[1];
      dts[d].innerText = localTime(dts[d].dataset.unix, id);
    }
  }
}

function timeAgo(timestamp) {
  var time = Math.round(Date.now()/1000);
  var el = time - timestamp;
  if (el==0) return "un instante";
  else if (el==1) return "un segundo";
  else if (el<60) return el + " segundos";
  else if (el<120) return "un minuto";
  else if (el<3600) return Math.round(el/60) + " minutos";
  else if (el<7200) return "una hora";
  else if (el<86400) return Math.round(el/3600) + " horas";
  else if (el<172800) return "un día";
  else if (el<2628000) return Math.round(el/86400) + " días";
  else if (el<5256000) return "un mes";
  else if (el<31536000) return Math.round(el/2628000) + " meses";
  else if (el>31535999) return "más de un año";
}

function localTime(timestamp, id) {
  id = id || 0;
  var lcl = new Date(timestamp*1000);
  lcl = ("0"+lcl.getDate()).slice(-2) + "/" + ("0" + (lcl.getMonth()+1)).slice(-2) + "/" + lcl.getFullYear().toString().slice(-2) + "(" + week[lcl.getDay()] + ")" + ("0"+lcl.getHours()).slice(-2) + ":" + ("0"+lcl.getMinutes()).slice(-2) + ":" + ("0"+lcl.getSeconds()).slice(-2)
  if (id) lcl = lcl + " " + id;
  return lcl;
}

/* 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 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);
	}
	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);
    }
  }
}

function setPassword(id) {
	if (document.getElementById(id).password)
    with (document.getElementById(id))
      if(!password.value) password.value = getPassword("weabot_password");
}

// 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 = this.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;
	}
  e.preventDefault();
}

function postClick(e) {
  e.preventDefault();
  var sel = window.getSelection().toString();
  if (sel) { sel=sel.replace(/^/gm, ">")+"\n"; sel="\n"+sel; }
  insert(">>" + this.textContent + 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.innerHTML="<em>Cargando...</em>";

	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 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].innerText) - parseInt(b.childNodes[1].innerText)) }); }
  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") {
    document.getElementById("header").style.display = "none";
    document.getElementById("content").className = "grid";
    var style = document.createElement("style");
    style.id = "labels";
    style.type = "text/css";
    style.innerText = '#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") {
    var style = document.getElementById("labels");
    if (style) style.parentNode.removeChild(style);
    document.getElementById("header").removeAttribute("style");
    document.getElementById("content").className = "list";
  }
  localStorage.setItem("threadlist", JSON.stringify(opcs));
}

function searchSubjects(e) {
  var filter = this.value.toLowerCase();
  var nodes = document.getElementsByClassName("thread");
  for (i=0; i<nodes.length; i++) {
    if (nodes[i].innerText.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);
}

function is_ie() { return(document.all&&!document.opera); }

function showpost(post) {
  post.children[0].classList.remove("hidden");
  post.children[1].style.display = 'block';
}
function hidepost(post) {
  post.children[0].classList.add("hidden");
  post.children[1].style.display = 'none';
}
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') {
    showpost(post);
    if(hiddenposts.includes(postid)) hiddenposts.splice(hiddenposts.indexOf(postid), 1);
  } else {
    hidepost(post);
    if(!hiddenposts.includes(postid)) hiddenposts.push(postid);
  }
  
  localStorage.setItem("hiddenposts", hiddenposts.join("!"));
}

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

  board = document.body.dataset.brd;

  var ids = document.getElementsByClassName("num");
  for(var i=0;i<ids.length;i++) ids[i].addEventListener("click", postClick);

  var forms = document.getElementsByTagName("form");
  for(var i=0;i<forms.length;i++) { if(forms[i].id.startsWith("postform")) setInputs(forms[i].id); }

  if (document.body.className == "mainpage") {
    var styleSelect = document.getElementById("styles");
    if (styleSelect) {
      styleSelect.addEventListener("change", applyTheme, false);
      styleSelect.value = localStorage.getItem(style_cookie);
    }

    var where = document.getElementById(board);
    if (where) where.className = "cur_brd";
  }

  if(localStorage.getItem("shobon_time") != "false")
    changeDate();

  var dds = document.getElementsByClassName("del");
  for(var i=0;i<dds.length;i++) { dds[i].children[1].addEventListener("click", deletePost); }

  if (document.body.className == "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(); }); }
    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(); }); }
    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("hiddenposts"))
    hiddenposts = localStorage.getItem("hiddenposts").split("!");
    
  var pps = document.getElementsByClassName("date");
  for(var i=0;i<pps.length;i++) {
    if(hiddenposts.includes(board+pps[i].dataset.unix)) {
      console.log(pps[i].dataset.unix);
      hidepost(pps[i].parentElement.parentElement);
    }
    pps[i].parentElement.addEventListener("dblclick", togglepost);
  }
});