Browse Source

Merge pull request #752 from mcspr/webui-host

WebUI url parsing
rfm69
Xose Pérez 6 years ago
committed by GitHub
parent
commit
f1ab288cc8
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 17 deletions
  1. +33
    -16
      code/html/custom.js
  2. +1
    -1
      code/html/index.html

+ 33
- 16
code/html/custom.js View File

@ -4,7 +4,8 @@ var maxNetworks;
var maxSchedules; var maxSchedules;
var messages = []; var messages = [];
var free_size = 0; var free_size = 0;
var webhost;
var urls = {};
var numChanged = 0; var numChanged = 0;
var numReboot = 0; var numReboot = 0;
@ -323,7 +324,7 @@ function doUpgrade() {
$.ajax({ $.ajax({
// Your server script to process the upload // Your server script to process the upload
url: webhost + "upgrade",
url: urls.upgrade.href,
type: "POST", type: "POST",
// Form data // Form data
@ -467,7 +468,7 @@ function doUpdate() {
} }
function doBackup() { function doBackup() {
document.getElementById("downloader").src = webhost + "config";
document.getElementById("downloader").src = urls.config.href;
return false; return false;
} }
@ -1292,22 +1293,22 @@ function hasChanged() {
// Init & connect // 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(); } if (websock) { websock.close(); }
websock = new WebSocket(wshost);
websock = new WebSocket(urls.ws.href);
websock.onmessage = function(evt) { websock.onmessage = function(evt) {
var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t")); var data = getJson(evt.data.replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/\t/g, "\\t"));
if (data) { 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() { $(function() {
initMessages(); initMessages();
@ -1359,6 +1374,8 @@ $(function() {
$(document).on("change", "input", hasChanged); $(document).on("change", "input", hasChanged);
$(document).on("change", "select", hasChanged); $(document).on("change", "select", hasChanged);
connect();
// don't autoconnect when opening from filesystem
if (window.location.protocol === "file:") { return; }
connectToCurrentURL();
}); });

+ 1
- 1
code/html/index.html View File

@ -66,7 +66,7 @@
<div id="layout" class="webmode"> <div id="layout" class="webmode">
<a href="#menu" id="menuLink" class="menu-link">
<a id="menuLink" class="menu-link">
<span></span> <span></span>
</a> </a>


Loading…
Cancel
Save