diff --git a/code/html/custom.js b/code/html/custom.js index 89b2d175..03e06eb4 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -4,7 +4,8 @@ var maxNetworks; var maxSchedules; var messages = []; var free_size = 0; -var webhost; + +var urls = {}; var numChanged = 0; var numReboot = 0; @@ -323,7 +324,7 @@ function doUpgrade() { $.ajax({ // Your server script to process the upload - url: webhost + "upgrade", + url: urls.upgrade.href, type: "POST", // Form data @@ -467,7 +468,7 @@ function doUpdate() { } function doBackup() { - document.getElementById("downloader").src = webhost + "config"; + document.getElementById("downloader").src = urls.config.href; return false; } @@ -1292,22 +1293,22 @@ function hasChanged() { // Init & connect // ----------------------------------------------------------------------------- -function connect(host) { +function initUrls(root) { - if (typeof host === "undefined") { - host = window.location.href.replace("#", ""); - } else { - if (host.indexOf("http") !== 0) { - host = "http://" + host + "/"; - } - } - if (host.indexOf("http") !== 0) { return; } + var paths = ["ws", "upgrade", "config"]; - webhost = host; - var wshost = host.replace("http", "ws") + "ws"; + urls["root"] = root; + paths.forEach(function(path) { + urls[path] = new URL(path, root); + }); + + urls.ws.protocol = "ws"; +} +function connectToURL(url) { + initUrls(url); if (websock) { websock.close(); } - websock = new WebSocket(wshost); + websock = new WebSocket(urls.ws.href); websock.onmessage = function(evt) { var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t")); if (data) { @@ -1316,6 +1317,20 @@ function connect(host) { }; } +function connect(host) { + + if (!host.startsWith("http:") && !host.startsWith("https:")) { + host = "http://" + host; + } + + connectToURL(new URL(host)); +} + +function connectToCurrentURL() { + connectToURL(new URL(window.location)); +} + + $(function() { initMessages(); @@ -1359,6 +1374,8 @@ $(function() { $(document).on("change", "input", hasChanged); $(document).on("change", "select", hasChanged); - connect(); + // don't autoconnect when opening from filesystem + if (window.location.protocol === "file:") { return; } + connectToCurrentURL(); }); diff --git a/code/html/index.html b/code/html/index.html index 2fb524b0..77926da9 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -66,7 +66,7 @@