aboutsummaryrefslogblamecommitdiff
path: root/static/js/home.js
blob: e395638d1d4ccdd4b7b34d59b17d44e832e6b2ab (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
                                                                                                                                                               


                         







                                                                     

            

                                      


          








                                                     

                    
                        







                                        
                 

                                                       











                                                  







                                                            


                                   

              









                                                
 

                                    
                                                           

                        





                                                                                                
                            
                                                     

                 





                                 
   




                                                
                   

                                     
                                                   
      
 
                        
 
                                                       



                                                      
 
                            








                                                            


                            






                                                          


                    


                        
                  
                                                                                   










                                               
                                                       




                                          






                                                                       



                                             


                              









                                                        


                        

     
  
 


                                                     

                                                          
                                               
 


                                                    
                                                  

            


               








                                                                                
 




                                                 







                            
   
console.log("%c¡Es calidad BaI!", "font-size:25px; font-weight:bold; text-decoration:underline; font-style:italic; text-decoration-line:underline overline;");

function check_news() {
  var last_t = opcs.last;
  var items = document.getElementsByClassName("ni");
  var dates = document.getElementsByClassName("ni-d");
  for (var i = 0; i < items.length; i++)
    if (parseInt(items[i].dataset.t) > last_t) {
      items[i].className += " urgent";
      dates[i].innerHTML =
        '<img src="/new.gif" style="width:18px;height:7px;"><br />' +
        dates[i].innerHTML;
    } else {
      break;
    }
  opcs.last = (Date.now() / 1000) | 0;
  parse();
}

function zeropad(n, w) {
  var n = String(n);
  if (n.length >= w) {
    return n;
  } else {
    return (String(0).repeat(w) + n).slice(n.length);
  }
}

var lastTime = 0;
var refreshInterval;
var refreshMaxTime = 10;
var refreshTime;
var unread = {};
var last_threads = 0;
var last_serverTime = 0;
var http_request = new XMLHttpRequest();

function loadJSON() {
  stopCounter("...");
  var data_file =
    "/cgi/api/lastage?time=" + lastTime +
    "&limit=" + document.getElementById("limit").value;
  http_request.open("GET", data_file, true);
  http_request.send();
}

function setRead(threadId) {
  if (threadId in unread) {
    unread[threadId] = false;
    updatePostList(last_threads, last_serverTime);
  }
}

function updatePostList(threads, serverTime) {
  if (refreshMaxTime >= 120 && refreshMaxTime <= 270) {
    refreshMaxTime += 30;
  } else if (refreshMaxTime >= 60 && refreshMaxTime < 120) {
    refreshMaxTime += 10;
  } else if (refreshMaxTime < 60) {
    refreshMaxTime += 5;
  }

  var arrayLength = threads.length;
  if (!arrayLength) return;

  divbbs = "";
  divib = "";
  last_threads = threads;
  last_serverTime = serverTime;

  var newposts = 0;
  var newTitle = "Bienvenido a Internet BBS/IB";
  var new_unread = false;
  var news = [];

  for (var i = 0; i < arrayLength; i++) {
    thread = threads[i];

    if (thread.bumped >= lastTime) {
      unread[thread.id] = true;
      news.push(thread.board_name + ": " + thread.content);
      new_unread = true;
    }

    html =
      '<a href="' + thread.url + '" class="thread">' +
      '<span class="brd">[' + thread.board_abr + ':' + zeropad(thread.length, 3) + ']</span> ' +
      thread.content + '</a>'

    if (unread[thread.id]) {
      html = '<span class="new">' + html + "</span>";
      newposts++;
    }

    if (thread.board_type == 1) {
      divbbs += html;
    } else {
      divib += html;
    }
  }

  if (newposts) {
    newTitle = "(" + newposts + ") " + newTitle;
  }

  if (new_unread) {
    notif(
      "Bienvenido a Internet BBS/IB",
      "Hay nuevos mensajes en:\n" + news.join("\n")
    );

    refreshMaxTime = 10;

    if (document.getElementById("autosound").checked) {
      document.getElementById("machina").volume = 0.6;
      document.getElementById("machina").play();
    }
  }

  document.title = newTitle;
  window.addEventListener('focus', markRead);

  document.getElementById("postlistbbs").innerHTML = divbbs;
  document.getElementById("postlistib").innerHTML = divib;
}

function markRead() {
  document.title = bainame;
  window.removeEventListener('focus', markRead);
}

function notif(title, msg) {
  if (Notification.permission === "granted") {
    var n = new Notification(title, {
      body: msg,
      icon: 'https://bienvenidoainternet.org/favicon.ico',
    });
    setTimeout(n.close.bind(n), 5000);
  }
}

function counter() {
  if (refreshTime < 1) {
    loadJSON();
  } else {
    refreshTime--;
    document.getElementById("counter").textContent = "(" + (refreshTime + 1) + ")";
  }
}

function startCounter() {
  refreshTime = refreshMaxTime;
  counter();
  refreshInterval = setInterval(counter, 1000);
}

function stopCounter(str) {
  clearInterval(refreshInterval);
  document.getElementById("counter").textContent = str;
}

function autoRefresh(e) {
  if (chk.checked) {
    if (chk_snd) chk_snd.disabled = false;

    if (!("Notification" in window)) {
      console.log("Alerta: Este navegador no soporta notificaciones.");
    } else {
      Notification.requestPermission();
    }

    lastTime = Math.floor(Date.now() / 1000);
    refreshTime = refreshMaxTime;
    startCounter();
  } else {
    if (chk_snd) {
      chk_snd.disabled = true;
    }
    stopCounter("");
  }
}

http_request.onreadystatechange = function() {
  if (http_request.readyState == 4) {
    var jsonObj = JSON.parse(http_request.responseText);
    if (jsonObj.state == "success") {
      updatePostList(jsonObj.threads, jsonObj.time);
      lastTime = jsonObj.time;
      if (chk.checked) {
        startCounter();
      }
    }
  }
};

function parse() {
  localStorage.setItem("home", JSON.stringify(opcs));
}

document.addEventListener("DOMContentLoaded", function() {
  var bainame = "Bienvenido a Internet BBS/IB";

  if (localStorage.hasOwnProperty("home"))
    opcs = JSON.parse(localStorage.getItem("home"));
  else {
    opcs = { auto: false, sound: false, last: 0 };
    parse();
  }

  check_news();

  document.getElementById("autorefresh").addEventListener("click", function(e) {
    opcs.auto = !opcs.auto;
    autoRefresh();
    parse();
  });
  document.getElementById("autosound").addEventListener("click", function(e) {
    opcs.sound = !opcs.sound;
    parse();
  });

  chk = document.getElementById("autorefresh");
  chk_snd = document.getElementById("autosound");
  if (opcs.auto) {
    chk.checked = true;
    autoRefresh();
  } else {
    chk.checked = false;
  }
  if (opcs.sound) {
    chk_snd.checked = true;
  } else {
    chk_snd.checked = false;
  }
});