From cc6225548c844615195cb37bfaac9e7ff8c270a8 Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 4 Apr 2018 16:57:24 +0300 Subject: [PATCH 1/3] Don't change url when touching menu button --- code/html/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/html/index.html b/code/html/index.html index 1cb1cc44..141f4dbf 100644 --- a/code/html/index.html +++ b/code/html/index.html @@ -66,7 +66,7 @@
- + From 2b1fc1f3542035b5bc9c444603332ba74105248b Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 4 Apr 2018 16:59:48 +0300 Subject: [PATCH 2/3] Local debugging without connect() --- code/html/custom.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/code/html/custom.js b/code/html/custom.js index 9a4150e0..b77f1655 100644 --- a/code/html/custom.js +++ b/code/html/custom.js @@ -1358,6 +1358,8 @@ $(function() { $(document).on("change", "input", hasChanged); $(document).on("change", "select", hasChanged); + // don't autoconnect when opening from filesystem + if (window.location.protocol === "file:") { return; } connect(); }); From 10c19793f50ea08b7fd57e4fed80ff61b3e0fb2b Mon Sep 17 00:00:00 2001 From: Maxim Prokhorov Date: Wed, 4 Apr 2018 17:48:32 +0300 Subject: [PATCH 3/3] Using URL instead of string manipulation --- code/html/custom.js | 47 ++++++++++++++++++++++++++++++--------------- 1 file changed, 31 insertions(+), 16 deletions(-) diff --git a/code/html/custom.js b/code/html/custom.js index b77f1655..4fa568e7 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; @@ -322,7 +323,7 @@ function doUpgrade() { $.ajax({ // Your server script to process the upload - url: webhost + "upgrade", + url: urls.upgrade.href, type: "POST", // Form data @@ -466,7 +467,7 @@ function doUpdate() { } function doBackup() { - document.getElementById("downloader").src = webhost + "config"; + document.getElementById("downloader").src = urls.config.href; return false; } @@ -1291,22 +1292,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) { @@ -1315,6 +1316,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(); @@ -1360,6 +1375,6 @@ $(function() { // don't autoconnect when opening from filesystem if (window.location.protocol === "file:") { return; } - connect(); + connectToCurrentURL(); });