notification update

This commit is contained in:
Tristan Schneider 2020-12-14 16:50:16 +01:00
parent 1e575c3a7f
commit baf0c2e887

View File

@ -82,6 +82,8 @@ $config_data = json_decode($raw_json_config, false);
<script> <script>
var previousTextLength; var previousTextLength;
var config_data;
var unread_words = 0;
document.getElementById("wordinput").focus(); document.getElementById("wordinput").focus();
@ -90,19 +92,22 @@ $config_data = json_decode($raw_json_config, false);
.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 (alertIfNew && text.length > previousTextLength) { if (text.length > previousTextLength) {
var newtxt = text.substring(previousTextLength, text.length); document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each") + " (" + ++unread_words + ")";
//var parser = new DOMParser(); if (alertIfNew) {
//var xmltxt = parser.parseFromString(newtxt, "text/xml"); var newtxt = text.substring(previousTextLength, text.length);
//var newword = xmltxt.getElementsByTagName("span")[0].innerHTML.trim(); //var parser = new DOMParser();
var htmltxt = document.createElement("html"); //var xmltxt = parser.parseFromString(newtxt, "text/xml");
htmltxt.innerHTML = newtxt; //var newword = xmltxt.getElementsByTagName("span")[0].innerHTML.trim();
var newword = htmltxt.getElementsByTagName("span")[0].innerHTML.trim(); var htmltxt = document.createElement("html");
if (Notification.permission == 'granted') { htmltxt.innerHTML = newtxt;
displayNotification(newword); var newword = htmltxt.getElementsByTagName("span")[0].innerHTML.trim();
} if (Notification.permission == 'granted') {
else { displayNotification(newword);
alert("Neues Wort: " + newword); }
else {
alert("Neues Wort: " + newword);
}
} }
} }
previousTextLength = text.length; previousTextLength = text.length;
@ -117,6 +122,14 @@ $config_data = json_decode($raw_json_config, false);
}); });
} }
function loadConfig() {
fetch("./data/config.json").then((response) => {
return response.text().then((text) => {
config_data = JSON.parse(text);
});
});
}
function onReloadClicked() { function onReloadClicked() {
loadSentences(); loadSentences();
document.querySelector("#but-reload").style.visibility = "hidden"; document.querySelector("#but-reload").style.visibility = "hidden";
@ -128,13 +141,19 @@ $config_data = json_decode($raw_json_config, false);
function displayNotification(word) { function displayNotification(word) {
if (Notification.permission == 'granted') { if (Notification.permission == 'granted') {
navigator.serviceWorker.getRegistration().then(function(reg) { navigator.serviceWorker.getRegistration().then(function(reg) {
reg.showNotification("Neues Wort auf OWE", { body: word, icon: "OWE_Logo.png", requireInteraction: true }); reg.showNotification("Neues Wort auf " + (config_data.title ? config_data.title : "OWE"),
{ body: word, icon: config_data.logo.image_path ? config_data.logo.image_path : "OWE_Logo.png", requireInteraction: true });
}); });
} }
} }
window.onfocus = function() {
unread_words = 0;
document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each");
}
//setTimeout(loadSentences, 500); //setTimeout(loadSentences, 500);
loadConfig();
loadSentences(); loadSentences();
setInterval(loadSentences, 5000, true); setInterval(loadSentences, 5000, true);