var previousTextLength; var config_data; var unread_words = 0; function loadSentences(alertIfNew = false, increaseUnread = true) { fetch("./data/sentences.txt", {headers: {"Cache-Control": "no-cache, no-store"}}) .then((response) => { return response.text().then((text) => { document.querySelector(".sentences").innerHTML = text; if (text.length > previousTextLength) { if (increaseUnread) { document.getElementsByTagName("title")[0].innerText = "(" + ++unread_words + ") " + (config_data.title ? config_data.title : "One Word Each") ; } if (alertIfNew) { var newtxt = text.substring(previousTextLength, text.length); //var parser = new DOMParser(); //var xmltxt = parser.parseFromString(newtxt, "text/xml"); //var newword = xmltxt.getElementsByTagName("span")[0].innerHTML.trim(); var htmltxt = document.createElement("html"); htmltxt.innerHTML = newtxt; var newword = htmltxt.getElementsByTagName("span")[0].innerHTML.trim(); if (Notification.permission == 'granted') { displayNotification(newword); } else { alert("Neues Wort: " + newword); } } } previousTextLength = text.length; // color var spans = document.querySelector(".sentences").getElementsByTagName("span"); // we expect this to be from the user who's turn it is now (3 users) var histurnspan = spans[spans.length - 3]; //console.log(histurnspan.getAttribute("style")); document.getElementById("wordinput").setAttribute("style", histurnspan.getAttribute("style")); initializeLoginArea(); }); }); } function loadConfig() { fetch("./data/config.json", {headers: {"Cache-Control": "no-cache, no-store"}}).then((response) => { return response.text().then((text) => { config_data = JSON.parse(text); setInterval(loadSentences, 5000, ((typeof config_data.notifications !== "undefined") ? config_data.notifications : true)); }); }).catch(() => { config_data = Object(); // leave empty }); } function onReloadClicked() { loadSentences(); document.querySelector("#but-reload").style.visibility = "hidden"; setTimeout(function(){ document.querySelector("#but-reload").style.visibility = "initial"; }, 100); } function sendWord() { var data = new FormData(); data.append('submitted', encodeURI(document.getElementById("wordinput").value)); data.append('user', getNextPlayer()); const Http = new XMLHttpRequest(); Http.open('POST', '.'); Http.onreadystatechange=(e)=>{ document.getElementById("wordinput").value = ""; loadSentences(false, false); resetUnread(); } Http.send(data); } function displayNotification(word) { if (Notification.permission == 'granted') { navigator.serviceWorker.getRegistration().then(function(reg) { reg.showNotification("Neues Wort auf " + (config_data.title ? config_data.title : "OWE"), { body: word, icon: (config_data.logo? config_data.logo.image_path : "OWE_Logo.png"), requireInteraction: true }); }); } } function toggleArchives() { archives = document.getElementsByClassName("archive_links")[0]; if (archives.style.display == "none") { archives.style.display = "flex"; if (config_data.archive) { document.getElementById("archive_image").src = config_data.archive.logo_path_open } return; } archives.style.display = "none"; if (config_data.archive) { document.getElementById("archive_image").src = config_data.archive.logo_path_closed } } function resetUnread() { unread_words = 0; document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each"); } function getNextPlayer() { var spans = document.querySelector(".sentences").getElementsByTagName("span"); var lastspan = spans[spans.length - 1]; var lastplayer = lastspan.dataset.user; var idx_next = (config_data.users.indexOf(lastplayer) + 1) % config_data.users.length; return config_data.users[idx_next]; } function getTimeSinceLast() { var spans = document.querySelector(".sentences").getElementsByTagName("span"); var lastspan = spans[spans.length - 1]; var lasttime = lastspan.dataset.time; return Date.now()/1e3 - lasttime; // in seconds } function initializeLoginArea() { var loginArea = document.getElementById('login_area'); //loginArea.innerHTML = "Eingeloggt als NOCH NICHT IMPLEMENTIERT"; loginArea.innerHTML = ""; // first clear it out loginArea.innerHTML += getNextPlayer() + " ist dran!
\n"; var relative_time = getTimeSinceLast(); var days = Math.floor(relative_time/(3600*24)); var hours = Math.floor(relative_time/(3600))%24; var minutes = Math.floor(relative_time/(60))%60; loginArea.innerHTML += "Schon seit " + days + " Tagen, " + hours + " Stunden und " + minutes + " Minuten!"; } window.onfocus = resetUnread; //setTimeout(loadSentences, 500); loadConfig(); loadSentences(); var input = document.getElementById("wordinput"); input.focus(); // Execute a function when the user releases a key on the keyboard input.addEventListener("keyup", function(event) { // Number 13 is the "Enter" key on the keyboard if (event.keyCode === 13) { // Cancel the default action, if needed event.preventDefault(); // Trigger the button element with a click document.getElementById("sendbut").click(); } }); var isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent); if (!isMobile) { archives = document.getElementsByClassName("archive_links")[0]; sentences = document.getElementsByClassName("sentences")[0]; scrollbox = document.getElementsByClassName("scrollbox")[0]; scrollbox.style.flexDirection = "row"; sentences.style.fontSize = "x-large"; archives.style.order = "1"; document.getElementById("sidebar_button").style.display = "inline"; } if ('serviceWorker' in navigator) { navigator.serviceWorker.register('./service-worker.js') .then(function(registration) { console.log('Registration successful, scope is:', registration.scope); }) .catch(function(error) { console.log('Service worker registration failed, error:', error); }); } navigator.serviceWorker.register('./service-worker.js', { scope: './' }); navigator.serviceWorker.register('./service-worker.js', { scope: '.' }); Notification.requestPermission(function(status) { console.log('Notification permission status:', status); });