Browse Source

Using URL instead of string manipulation

rfm69
Maxim Prokhorov 6 years ago
parent
commit
10c19793f5
1 changed files with 31 additions and 16 deletions
  1. +31
    -16
      code/html/custom.js

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

@ -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();
});

Loading…
Cancel
Save