aboutsummaryrefslogtreecommitdiff
path: root/static/js/autorefresh.js
diff options
context:
space:
mode:
authorLibravatar Choom 2023-01-28 00:52:56 -0300
committerLibravatar Choom 2023-02-02 00:39:27 -0300
commit081ddc03aef7becc3b084a9079a2574fd3e3b49b (patch)
treea6ca793a0c21917d3774778c0a14f65a8c678113 /static/js/autorefresh.js
parentd0d6b8ebff31d2306a040dc9c53f9e5e7a0ec7a0 (diff)
downloadweabot-081ddc03aef7becc3b084a9079a2574fd3e3b49b.tar.gz
weabot-081ddc03aef7becc3b084a9079a2574fd3e3b49b.tar.xz
weabot-081ddc03aef7becc3b084a9079a2574fd3e3b49b.zip
userconf
Diffstat (limited to 'static/js/autorefresh.js')
-rw-r--r--static/js/autorefresh.js83
1 files changed, 41 insertions, 42 deletions
diff --git a/static/js/autorefresh.js b/static/js/autorefresh.js
index d804675..e3fb4b6 100644
--- a/static/js/autorefresh.js
+++ b/static/js/autorefresh.js
@@ -22,17 +22,13 @@ function loadJSON() {
if (manual) document.getElementById("counter").innerText = "...";
var data_file;
if (serviceType == 2 || serviceType == 3) {
- board = document.getElementsByName("board")[0].value;
+ board = document.body.dataset.brd;
parent = document.getElementsByName("parent")[0].value;
data_file =
- "/cgi/api/thread?dir=" +
- board +
- "&id=" +
- parent +
- "&offset=" +
- thread_length +
- "&time=" +
- lastTime;
+ "/cgi/api/thread?dir=" + board
+ + "&id=" + parent
+ + "&offset=" + thread_length
+ + "&time=" + lastTime;
} else {
return false;
}
@@ -216,27 +212,35 @@ function updateThread(posts, total_replies, serverTime) {
threadDiv.setAttribute("data-length", num);
- if (serviceType == 2)
+ if (serviceType == 2) {
document
.getElementsByTagName("h3")[0]
.getElementsByTagName("span")[0].innerText = "(" + num + " respuestas)";
+ }
}
if (posts.length > 0) {
if (!manual) refreshMaxTime = 10;
if (!document.hasFocus())
- if (posts.length > 1)
+ if (posts.length > 1) {
notif(thread_title, posts.length + " nuevos mensajes");
- else notif(thread_title, "Un nuevo mensaje");
+ } else {
+ notif(thread_title, "Un nuevo mensaje");
+ }
} else {
- if (refreshMaxTime <= 60) refreshMaxTime += 5;
+ if (refreshMaxTime <= 60) {
+ refreshMaxTime += 5;
+ }
}
thread_length = parseInt(total_replies) + 1;
new_unread = thread_length - thread_first_length;
- if (new_unread) document.title = "(" + new_unread + ") " + thread_title;
- else document.title = thread_title;
+ if (new_unread) {
+ document.title = "(" + new_unread + ") " + thread_title;
+ } else {
+ document.title = thread_title;
+ }
}
function notif(title, msg) {
@@ -263,9 +267,9 @@ function detectService() {
document.getElementsByClassName("thread")[0].dataset.length
);
thread_first_length = thread_length;
- replylist = document.getElementsByClassName("reply");
- lastr = replylist[replylist.length - 1].textContent;
- thread_lastreply = parseInt(lastr.substr(0, lastr.indexOf(" :")));
+ thread_lastreply = parseInt(
+ document.getElementsByClassName("replies")[0].lastElementChild.dataset.n
+ );
if (thread_length == thread_lastreply) {
serviceType = 2;
document.getElementById("n").addEventListener("click", checkNew);
@@ -300,8 +304,6 @@ function detectService() {
document.getElementsByClassName("thread")[0].dataset.length
);
thread_first_length = thread_length;
- replylist = document.getElementsByClassName("thread");
- replylist += document.getElementsByClassName("reply");
var footer = document.getElementsByClassName("nav")[1];
var mnl = document.createElement("a");
mnl.id = "shownew";
@@ -346,15 +348,12 @@ function stopCounter(str) {
}
function autoRefresh(e) {
- chk_snd = document.getElementById("autosound");
if (document.getElementById("autorefresh").checked) {
- if (chk_snd) chk_snd.disabled = false;
Notification.requestPermission();
lastTime = Math.floor(Date.now() / 1000);
refreshTime = refreshMaxTime;
startCounter();
} else {
- if (chk_snd) document.getElementById("autosound").disabled = true;
stopCounter("OFF");
}
}
@@ -374,31 +373,31 @@ http_request.onreadystatechange = function() {
manual = 0;
}
};
+
+function setCookie(e) {
+ if (chk.checked) {
+ weabot.autorefresh = true;
+ } else {
+ weabot.autorefresh = false;
+ }
+ localStorage.setItem("weabot", JSON.stringify(weabot));
+}
+
document.addEventListener("DOMContentLoaded", function() {
if (!detectService()) return;
+ if (localStorage.hasOwnProperty("weabot")) {
+ weabot = JSON.parse(localStorage.getItem("weabot"));
+ } else {
+ weabot = { autorefresh: false };
+ }
+
chk = document.getElementById("autorefresh");
- chk_snd = document.getElementById("autosound");
- if (localStorage.getItem("autorefresh")) {
- document.getElementById("autorefresh").checked = true;
+ if (weabot.autorefresh) {
+ chk.checked = true;
autoRefresh();
}
- if (!chk_snd) return;
- if (localStorage.getItem("mainpage_nosound"))
- document.getElementById("autosound").checked = false;
-});
-
-window.addEventListener("unload", function() {
- if (!serviceType) return;
-
- chk = document.getElementById("autorefresh");
- chk_snd = document.getElementById("autosound");
- if (chk.checked) localStorage.setItem("autorefresh", true);
- else localStorage.removeItem("autorefresh");
- if (!chk_snd) return;
- if (!document.getElementById("autosound").checked)
- localStorage.setItem("mainpage_nosound", true);
- else localStorage.removeItem("mainpage_nosound");
+ chk.addEventListener('change', setCookie);
});