update on new word counter and notifications

This commit is contained in:
Tristan Schneider 2021-02-13 21:31:27 +01:00
parent ba580d9b1e
commit a9f2c9f8b9

11
owe.js
View File

@ -2,13 +2,15 @@ var previousTextLength;
var config_data; var config_data;
var unread_words = 0; var unread_words = 0;
function loadSentences(alertIfNew = false) { function loadSentences(alertIfNew = false, increaseUnread = true) {
fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}}) fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}})
.then((response) => { .then((response) => {
return response.text().then((text) => { return response.text().then((text) => {
document.querySelector(".sentences").innerHTML = text; document.querySelector(".sentences").innerHTML = text;
if (text.length > previousTextLength) { if (text.length > previousTextLength) {
document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each") + " (" + ++unread_words + ")"; if (increaseUnread) {
document.getElementsByTagName("title")[0].innerText = "(" + ++unread_words + ") " + (config_data.title ? config_data.title : "One Word Each") ;
}
if (alertIfNew) { if (alertIfNew) {
var newtxt = text.substring(previousTextLength, text.length); var newtxt = text.substring(previousTextLength, text.length);
//var parser = new DOMParser(); //var parser = new DOMParser();
@ -41,6 +43,7 @@ function loadConfig() {
fetch("./data/config.json").then((response) => { fetch("./data/config.json").then((response) => {
return response.text().then((text) => { return response.text().then((text) => {
config_data = JSON.parse(text); config_data = JSON.parse(text);
setInterval(loadSentences, 5000, ((typeof config_data.notifications !== "undefined") ? config_data.notifications : true));
}); });
}).catch(() => { }).catch(() => {
config_data = Object(); // leave empty config_data = Object(); // leave empty
@ -62,7 +65,8 @@ function sendWord() {
Http.open('POST', '.'); Http.open('POST', '.');
Http.onreadystatechange=(e)=>{ Http.onreadystatechange=(e)=>{
document.getElementById("wordinput").value = ""; document.getElementById("wordinput").value = "";
loadSentences(); loadSentences(false, false);
resetUnread();
} }
Http.send(data); Http.send(data);
} }
@ -96,7 +100,6 @@ window.onfocus = resetUnread;
//setTimeout(loadSentences, 500); //setTimeout(loadSentences, 500);
loadConfig(); loadConfig();
loadSentences(); loadSentences();
setInterval(loadSentences, 5000, true);
var input = document.getElementById("wordinput"); var input = document.getElementById("wordinput");
input.focus(); input.focus();