2018-08-27 12:30:50 +00:00
|
|
|
/*
|
|
|
|
* noVNC: HTML5 VNC client
|
2019-06-24 11:26:25 +00:00
|
|
|
* Copyright (C) 2018 The noVNC Authors
|
2018-08-27 12:30:50 +00:00
|
|
|
* Licensed under MPL 2.0 (see LICENSE.txt)
|
|
|
|
*
|
|
|
|
* See README.md for usage and integration instructions.
|
|
|
|
*/
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
import { init_logging as main_init_logging } from '../core/util/logging.js';
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// init log level reading the logging HTTP param
|
2019-06-24 11:26:25 +00:00
|
|
|
export function init_logging(level) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
|
|
|
if (typeof level !== "undefined") {
|
2018-08-27 12:30:50 +00:00
|
|
|
main_init_logging(level);
|
2018-08-14 12:11:49 +00:00
|
|
|
} else {
|
2019-06-24 11:26:25 +00:00
|
|
|
const param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
|
2018-08-27 12:30:50 +00:00
|
|
|
main_init_logging(param || undefined);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Read a query string variable
|
2019-06-24 11:26:25 +00:00
|
|
|
export function getQueryVar(name, defVal) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
|
2018-08-14 12:11:49 +00:00
|
|
|
match = document.location.href.match(re);
|
2018-08-27 12:30:50 +00:00
|
|
|
if (typeof defVal === 'undefined') { defVal = null; }
|
2019-06-24 11:26:25 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
if (match) {
|
|
|
|
return decodeURIComponent(match[1]);
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
|
|
|
return defVal;
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Read a hash fragment variable
|
2019-06-24 11:26:25 +00:00
|
|
|
export function getHashVar(name, defVal) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
const re = new RegExp('.*[&#]' + name + '=([^&]*)'),
|
2018-08-14 12:11:49 +00:00
|
|
|
match = document.location.hash.match(re);
|
2018-08-27 12:30:50 +00:00
|
|
|
if (typeof defVal === 'undefined') { defVal = null; }
|
2019-06-24 11:26:25 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
if (match) {
|
|
|
|
return decodeURIComponent(match[1]);
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
|
|
|
return defVal;
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// Read a variable from the fragment or the query string
|
|
|
|
// Fragment takes precedence
|
2019-06-24 11:26:25 +00:00
|
|
|
export function getConfigVar(name, defVal) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
const val = getHashVar(name);
|
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
if (val === null) {
|
2019-06-24 11:26:25 +00:00
|
|
|
return getQueryVar(name, defVal);
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
return val;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
|
|
|
|
*/
|
|
|
|
|
|
|
|
// No days means only for this browser session
|
2019-06-24 11:26:25 +00:00
|
|
|
export function createCookie(name, value, days) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
let date, expires;
|
2018-08-14 12:11:49 +00:00
|
|
|
if (days) {
|
|
|
|
date = new Date();
|
2018-08-27 12:30:50 +00:00
|
|
|
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
|
2018-08-14 12:11:49 +00:00
|
|
|
expires = "; expires=" + date.toGMTString();
|
|
|
|
} else {
|
|
|
|
expires = "";
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
let secure;
|
2018-08-14 12:11:49 +00:00
|
|
|
if (document.location.protocol === "https:") {
|
|
|
|
secure = "; secure";
|
|
|
|
} else {
|
|
|
|
secure = "";
|
|
|
|
}
|
|
|
|
document.cookie = name + "=" + value + expires + "; path=/" + secure;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function readCookie(name, defaultValue) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
const nameEQ = name + "=";
|
|
|
|
const ca = document.cookie.split(';');
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
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);
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
2018-08-27 12:30:50 +00:00
|
|
|
return (typeof defaultValue !== 'undefined') ? defaultValue : null;
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function eraseCookie(name) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
|
|
|
createCookie(name, "", -1);
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Setting handling.
|
|
|
|
*/
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
let settings = {};
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function initSettings() {
|
|
|
|
if (!window.chrome || !window.chrome.storage) {
|
|
|
|
settings = {};
|
|
|
|
return Promise.resolve();
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// No days means only for this browser session
|
2019-06-24 11:26:25 +00:00
|
|
|
export function writeSetting(name, value) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
if (settings[name] === value) return;
|
|
|
|
settings[name] = value;
|
2018-08-14 12:11:49 +00:00
|
|
|
if (window.chrome && window.chrome.storage) {
|
2019-06-24 11:26:25 +00:00
|
|
|
window.chrome.storage.sync.set(settings);
|
2018-08-14 12:11:49 +00:00
|
|
|
} else {
|
|
|
|
localStorage.setItem(name, value);
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function readSetting(name, defaultValue) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
let value;
|
|
|
|
if ((name in settings) || (window.chrome && window.chrome.storage)) {
|
2018-08-14 12:11:49 +00:00
|
|
|
value = settings[name];
|
|
|
|
} else {
|
|
|
|
value = localStorage.getItem(name);
|
2019-06-24 11:26:25 +00:00
|
|
|
settings[name] = value;
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
|
|
|
if (typeof value === "undefined") {
|
|
|
|
value = null;
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
2018-08-14 12:11:49 +00:00
|
|
|
if (value === null && typeof defaultValue !== "undefined") {
|
|
|
|
return defaultValue;
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function eraseSetting(name) {
|
2018-08-14 12:11:49 +00:00
|
|
|
"use strict";
|
2019-06-24 11:26:25 +00:00
|
|
|
// 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];
|
2018-08-14 12:11:49 +00:00
|
|
|
if (window.chrome && window.chrome.storage) {
|
|
|
|
window.chrome.storage.sync.remove(name);
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem(name);
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
export function injectParamIfMissing(path, param, value) {
|
2018-08-14 12:11:49 +00:00
|
|
|
// force pretend that we're dealing with a relative path
|
|
|
|
// (assume that we wanted an extra if we pass one in)
|
|
|
|
path = "/" + path;
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
const elem = document.createElement('a');
|
2018-08-14 12:11:49 +00:00
|
|
|
elem.href = path;
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
const param_eq = encodeURIComponent(param) + "=";
|
|
|
|
let query;
|
2018-08-14 12:11:49 +00:00
|
|
|
if (elem.search) {
|
|
|
|
query = elem.search.slice(1).split('&');
|
|
|
|
} else {
|
|
|
|
query = [];
|
|
|
|
}
|
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
if (!query.some(v => v.startsWith(param_eq))) {
|
2018-08-14 12:11:49 +00:00
|
|
|
query.push(param_eq + encodeURIComponent(value));
|
|
|
|
elem.search = "?" + query.join("&");
|
|
|
|
}
|
|
|
|
|
|
|
|
// some browsers (e.g. IE11) may occasionally omit the leading slash
|
|
|
|
// in the elem.pathname string. Handle that case gracefully.
|
|
|
|
if (elem.pathname.charAt(0) == "/") {
|
|
|
|
return elem.pathname.slice(1) + elem.search + elem.hash;
|
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
|
|
|
|
return elem.pathname + elem.search + elem.hash;
|
|
|
|
}
|
2018-08-14 12:11:49 +00:00
|
|
|
|
|
|
|
// 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.
|
2019-06-24 11:26:25 +00:00
|
|
|
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));
|
2018-08-14 12:11:49 +00:00
|
|
|
}
|
2019-06-24 11:26:25 +00:00
|
|
|
};
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
req.onerror = evt => reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message));
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'"));
|
2018-08-14 12:11:49 +00:00
|
|
|
|
2019-06-24 11:26:25 +00:00
|
|
|
req.send();
|
|
|
|
});
|
2018-08-27 12:30:50 +00:00
|
|
|
}
|