make script external

This commit is contained in:
Tristan Schneider 2020-12-25 13:06:00 +01:00
parent 1b00af7e3c
commit de484dcfdf
2 changed files with 100 additions and 104 deletions

105
index.php
View File

@ -94,109 +94,6 @@ $config_data = json_decode($raw_json_config, false);
</button> </button>
</div> </div>
<script src="owe.js?neu"></script>
<script>
var previousTextLength;
var config_data;
var unread_words = 0;
document.getElementById("wordinput").focus();
function loadSentences(alertIfNew = false) {
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) {
document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each") + " (" + ++unread_words + ")";
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"));
});
});
}
function loadConfig() {
fetch("./data/config.json").then((response) => {
return response.text().then((text) => {
config_data = JSON.parse(text);
});
}).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 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 });
});
}
}
window.onfocus = function() {
unread_words = 0;
document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each");
}
//setTimeout(loadSentences, 500);
loadConfig();
loadSentences();
setInterval(loadSentences, 5000, true);
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);
});
</script>
</body> </body>
</html> </html>

99
owe.js Normal file
View File

@ -0,0 +1,99 @@
var previousTextLength;
var config_data;
var unread_words = 0;
document.getElementById("wordinput").focus();
function loadSentences(alertIfNew = false) {
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) {
document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each") + " (" + ++unread_words + ")";
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"));
});
});
}
function loadConfig() {
fetch("./data/config.json").then((response) => {
return response.text().then((text) => {
config_data = JSON.parse(text);
});
}).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 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 });
});
}
}
window.onfocus = function() {
unread_words = 0;
document.getElementsByTagName("title")[0].innerText = (config_data.title ? config_data.title : "One Word Each");
}
//setTimeout(loadSentences, 500);
loadConfig();
loadSentences();
setInterval(loadSentences, 5000, true);
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);
});