/*
* noVNC: HTML5 VNC client
- * Copyright (C) 2012 Joel Martin
- * Copyright (C) 2013 NTT corp.
+ * Copyright (C) 2019 The noVNC Authors
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
-/*jslint bitwise: false, white: false, browser: true, devel: true */
-/*global Util, window, document */
-
-/* [module]
- * import Util from "../core/util";
- */
-
-// Globals defined here
-var WebUtil = {};
-
-/*
- * ------------------------------------------------------
- * Namespaced in WebUtil
- * ------------------------------------------------------
- */
+import { initLogging as mainInitLogging } from '../core/util/logging.js';
// init log level reading the logging HTTP param
-WebUtil.init_logging = function (level) {
+export function initLogging(level) {
"use strict";
if (typeof level !== "undefined") {
- Util._log_level = level;
+ mainInitLogging(level);
} else {
- var param = document.location.href.match(/logging=([A-Za-z0-9\._\-]*)/);
- Util._log_level = (param || ['', Util._log_level])[1];
+ const param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
+ mainInitLogging(param || undefined);
}
- Util.init_logging();
-};
-
-
-WebUtil.dirObj = function (obj, depth, parent) {
- "use strict";
- if (! depth) { depth = 2; }
- if (! parent) { parent = ""; }
-
- // Print the properties of the passed-in object
- var msg = "";
- for (var i in obj) {
- if ((depth > 1) && (typeof obj[i] === "object")) {
- // Recurse attributes that are objects
- msg += WebUtil.dirObj(obj[i], depth - 1, parent + "." + i);
- } else {
- //val = new String(obj[i]).replace("\n", " ");
- var val = "";
- if (typeof(obj[i]) === "undefined") {
- val = "undefined";
- } else {
- val = obj[i].toString().replace("\n", " ");
- }
- if (val.length > 30) {
- val = val.substr(0, 30) + "...";
- }
- msg += parent + "." + i + ": " + val + "\n";
- }
- }
- return msg;
-};
+}
// Read a query string variable
-WebUtil.getQueryVar = function (name, defVal) {
+export function getQueryVar(name, defVal) {
"use strict";
- var re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
+ const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
match = document.location.href.match(re);
if (typeof defVal === 'undefined') { defVal = null; }
+
if (match) {
return decodeURIComponent(match[1]);
- } else {
- return defVal;
}
-};
+
+ return defVal;
+}
// Read a hash fragment variable
-WebUtil.getHashVar = function (name, defVal) {
+export function getHashVar(name, defVal) {
"use strict";
- var re = new RegExp('.*[&#]' + name + '=([^&]*)'),
+ const re = new RegExp('.*[&#]' + name + '=([^&]*)'),
match = document.location.hash.match(re);
if (typeof defVal === 'undefined') { defVal = null; }
+
if (match) {
return decodeURIComponent(match[1]);
- } else {
- return defVal;
}
-};
+
+ return defVal;
+}
// Read a variable from the fragment or the query string
// Fragment takes precedence
-WebUtil.getConfigVar = function (name, defVal) {
+export function getConfigVar(name, defVal) {
"use strict";
- var val = WebUtil.getHashVar(name);
+ const val = getHashVar(name);
+
if (val === null) {
- val = WebUtil.getQueryVar(name, defVal);
+ return getQueryVar(name, defVal);
}
+
return val;
-};
+}
/*
* Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
*/
// No days means only for this browser session
-WebUtil.createCookie = function (name, value, days) {
+export function createCookie(name, value, days) {
"use strict";
- var date, expires;
+ let date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "";
}
- var secure;
+ let secure;
if (document.location.protocol === "https:") {
secure = "; secure";
} else {
secure = "";
}
document.cookie = name + "=" + value + expires + "; path=/" + secure;
-};
+}
-WebUtil.readCookie = function (name, defaultValue) {
+export function readCookie(name, defaultValue) {
"use strict";
- var nameEQ = name + "=",
- ca = document.cookie.split(';');
+ const nameEQ = name + "=";
+ const ca = document.cookie.split(';');
- for (var i = 0; i < ca.length; i += 1) {
- var c = ca[i];
- while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
- if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
+ for (let i = 0; i < ca.length; i += 1) {
+ let c = ca[i];
+ while (c.charAt(0) === ' ') {
+ c = c.substring(1, c.length);
+ }
+ if (c.indexOf(nameEQ) === 0) {
+ return c.substring(nameEQ.length, c.length);
+ }
}
+
return (typeof defaultValue !== 'undefined') ? defaultValue : null;
-};
+}
-WebUtil.eraseCookie = function (name) {
+export function eraseCookie(name) {
"use strict";
- WebUtil.createCookie(name, "", -1);
-};
+ createCookie(name, "", -1);
+}
/*
* Setting handling.
*/
-WebUtil.initSettings = function (callback /*, ...callbackArgs */) {
- "use strict";
- var callbackArgs = Array.prototype.slice.call(arguments, 1);
- if (window.chrome && window.chrome.storage) {
- window.chrome.storage.sync.get(function (cfg) {
- WebUtil.settings = cfg;
- console.log(WebUtil.settings);
- if (callback) {
- callback.apply(this, callbackArgs);
- }
- });
- } else {
- // No-op
- if (callback) {
- callback.apply(this, callbackArgs);
- }
+let settings = {};
+
+export function initSettings() {
+ if (!window.chrome || !window.chrome.storage) {
+ settings = {};
+ return Promise.resolve();
}
-};
+
+ return new Promise(resolve => window.chrome.storage.sync.get(resolve))
+ .then((cfg) => { settings = cfg; });
+}
+
+// Update the settings cache, but do not write to permanent storage
+export function setSetting(name, value) {
+ settings[name] = value;
+}
// No days means only for this browser session
-WebUtil.writeSetting = function (name, value) {
+export function writeSetting(name, value) {
"use strict";
+ if (settings[name] === value) return;
+ settings[name] = value;
if (window.chrome && window.chrome.storage) {
- //console.log("writeSetting:", name, value);
- if (WebUtil.settings[name] !== value) {
- WebUtil.settings[name] = value;
- window.chrome.storage.sync.set(WebUtil.settings);
- }
+ window.chrome.storage.sync.set(settings);
} else {
localStorage.setItem(name, value);
}
-};
+}
-WebUtil.readSetting = function (name, defaultValue) {
+export function readSetting(name, defaultValue) {
"use strict";
- var value;
- if (window.chrome && window.chrome.storage) {
- value = WebUtil.settings[name];
+ let value;
+ if ((name in settings) || (window.chrome && window.chrome.storage)) {
+ value = settings[name];
} else {
value = localStorage.getItem(name);
+ settings[name] = value;
}
if (typeof value === "undefined") {
value = null;
}
- if (value === null && typeof defaultValue !== undefined) {
+
+ if (value === null && typeof defaultValue !== "undefined") {
return defaultValue;
- } else {
- return value;
}
-};
-WebUtil.eraseSetting = function (name) {
+ return value;
+}
+
+export function eraseSetting(name) {
"use strict";
+ // Deleting here means that next time the setting is read when using local
+ // storage, it will be pulled from local storage again.
+ // If the setting in local storage is changed (e.g. in another tab)
+ // between this delete and the next read, it could lead to an unexpected
+ // value change.
+ delete settings[name];
if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.remove(name);
- delete WebUtil.settings[name];
} else {
localStorage.removeItem(name);
}
-};
-
-/*
- * Alternate stylesheet selection
- */
-WebUtil.getStylesheets = function () {
- "use strict";
- var links = document.getElementsByTagName("link");
- var sheets = [];
-
- for (var i = 0; i < links.length; i += 1) {
- if (links[i].title &&
- links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
- sheets.push(links[i]);
- }
- }
- return sheets;
-};
-
-// No sheet means try and use value from cookie, null sheet used to
-// clear all alternates.
-WebUtil.selectStylesheet = function (sheet) {
- "use strict";
- if (typeof sheet === 'undefined') {
- sheet = 'default';
- }
-
- var sheets = WebUtil.getStylesheets();
- for (var i = 0; i < sheets.length; i += 1) {
- var link = sheets[i];
- if (link.title === sheet) {
- Util.Debug("Using stylesheet " + sheet);
- link.disabled = false;
- } else {
- //Util.Debug("Skipping stylesheet " + link.title);
- link.disabled = true;
- }
- }
- return sheet;
-};
+}
-WebUtil.injectParamIfMissing = function (path, param, value) {
+export function injectParamIfMissing(path, param, value) {
// force pretend that we're dealing with a relative path
// (assume that we wanted an extra if we pass one in)
path = "/" + path;
- var elem = document.createElement('a');
+ const elem = document.createElement('a');
elem.href = path;
- var param_eq = encodeURIComponent(param) + "=";
- var query;
+ const paramEq = encodeURIComponent(param) + "=";
+ let query;
if (elem.search) {
query = elem.search.slice(1).split('&');
} else {
query = [];
}
- if (!query.some(function (v) { return v.startsWith(param_eq); })) {
- query.push(param_eq + encodeURIComponent(value));
+ if (!query.some(v => v.startsWith(paramEq))) {
+ query.push(paramEq + encodeURIComponent(value));
elem.search = "?" + query.join("&");
}
// in the elem.pathname string. Handle that case gracefully.
if (elem.pathname.charAt(0) == "/") {
return elem.pathname.slice(1) + elem.search + elem.hash;
- } else {
- return elem.pathname + elem.search + elem.hash;
}
-};
-/* [module] export default WebUtil; */
+ return elem.pathname + elem.search + elem.hash;
+}
+
+// sadly, we can't use the Fetch API until we decide to drop
+// IE11 support or polyfill promises and fetch in IE11.
+// resolve will receive an object on success, while reject
+// will receive either an event or an error on failure.
+export function fetchJSON(path) {
+ return new Promise((resolve, reject) => {
+ // NB: IE11 doesn't support JSON as a responseType
+ const req = new XMLHttpRequest();
+ req.open('GET', path);
+
+ req.onload = () => {
+ if (req.status === 200) {
+ let resObj;
+ try {
+ resObj = JSON.parse(req.responseText);
+ } catch (err) {
+ reject(err);
+ }
+ resolve(resObj);
+ } else {
+ reject(new Error("XHR got non-200 status while trying to load '" + path + "': " + req.status));
+ }
+ };
+
+ req.onerror = evt => reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message));
+
+ req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'"));
+
+ req.send();
+ });
+}