Update script.js

This commit is contained in:
Kroese 2025-10-08 14:31:42 +02:00 committed by GitHub
parent 4cc5cfae12
commit dc4c93ce41
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -6,7 +6,6 @@ function getInfo() {
var url = "msg.html";
try {
if (window.XMLHttpRequest) {
request = new XMLHttpRequest();
} else {
@ -18,14 +17,36 @@ function getInfo() {
request.send();
} catch (e) {
var err = "Error: " + e.message;
console.log(err);
setError(err);
setError("Error: " + e.message);
}
}
function getURL() {
var protocol = window.location.protocol === "https:" ? "wss:" : "ws:";
var path = window.location.pathname.replace(/[^/]*$/, '').replace(/\/$/, '');
return protocol + "//" + window.location.host + path;
}
function processMsg(msg) {
if (msg.toLowerCase().indexOf("href=") !== -1) {
var div = document.createElement("div");
div.innerHTML = msg;
var url = div.querySelector("a").href;
setTimeout(() => {
window.location.assign(url);
}, 3000);
}
setInfo(msg);
return true;
}
function processInfo() {
try {
if (request.readyState != 4) {
return true;
}
@ -33,7 +54,7 @@ function processInfo() {
var msg = request.responseText;
if (msg == null || msg.length == 0) {
setInfo("Booting DSM instance", true);
schedule();
setTimeout(getInfo, interval);
return false;
}
@ -43,20 +64,11 @@ function processInfo() {
if (msg.toLowerCase().indexOf("<html>") !== -1) {
notFound = true;
} else {
if (msg.toLowerCase().indexOf("href=") !== -1) {
var div = document.createElement("div");
div.innerHTML = msg;
var url = div.querySelector("a").href;
setTimeout(() => {
window.location.assign(url);
}, 3000);
setInfo(msg);
return true;
} else {
setInfo(msg);
schedule();
return true;
processMsg(msg);
if (msg.toLowerCase().indexOf("href=") == -1) {
setTimeout(getInfo, interval);
}
return true;
}
}
@ -67,20 +79,17 @@ function processInfo() {
}
setError("Error: Received statuscode " + request.status);
schedule();
return false;
} catch (e) {
var err = "Error: " + e.message;
console.log(err);
setError(err);
setError("Error: " + e.message);
return false;
}
}
function setInfo(msg, loading, error) {
try {
if (msg == null || msg.length == 0) {
return false;
}
@ -114,17 +123,50 @@ function setInfo(msg, loading, error) {
}
function setError(text) {
console.warn(text);
return setInfo(text, false, true);
}
function schedule() {
setTimeout(getInfo, interval);
}
function reload() {
setTimeout(() => {
document.location.reload();
}, 3000);
}
schedule();
function connect() {
var wsUrl = getURL() + "/status";
var ws = new WebSocket(wsUrl);
ws.onmessage = function(e) {
var pos = e.data.indexOf(":");
var cmd = e.data.substring(0, pos);
var msg = e.data.substring(pos + 2);
switch (cmd) {
case "s":
processMsg(msg);
break;
case "e":
setError(msg);
break;
default:
console.warn("Unknown event: " + cmd);
break;
}
};
ws.onclose = function(e) {
setTimeout(function() {
connect();
}, interval);
};
ws.onerror = function(e) {
ws.close();
};
}
getInfo();
connect();