1
0
Fork 0
mirror of https://github.com/retspen/webvirtcloud synced 2024-12-27 16:45:22 +00:00
webvirtcloud/static/js/novnc/app/webutil.js

240 lines
6.5 KiB
JavaScript
Raw Normal View History

/*
* noVNC: HTML5 VNC client
2020-08-11 12:05:09 +00:00
* Copyright (C) 2019 The noVNC Authors
* Licensed under MPL 2.0 (see LICENSE.txt)
*
* See README.md for usage and integration instructions.
*/
2020-08-11 12:05:09 +00:00
import { initLogging as mainInitLogging } from '../core/util/logging.js';
// init log level reading the logging HTTP param
2020-08-11 12:05:09 +00:00
export function initLogging(level) {
"use strict";
if (typeof level !== "undefined") {
2020-08-11 12:05:09 +00:00
mainInitLogging(level);
} else {
2019-06-24 11:26:25 +00:00
const param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
2020-08-11 12:05:09 +00:00
mainInitLogging(param || undefined);
}
2019-06-24 11:26:25 +00:00
}
// Read a query string variable
2019-06-24 11:26:25 +00:00
export function getQueryVar(name, defVal) {
"use strict";
2019-06-24 11:26:25 +00:00
const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
match = document.location.href.match(re);
if (typeof defVal === 'undefined') { defVal = null; }
2019-06-24 11:26:25 +00:00
if (match) {
return decodeURIComponent(match[1]);
}
2019-06-24 11:26:25 +00:00
return defVal;
}
// Read a hash fragment variable
2019-06-24 11:26:25 +00:00
export function getHashVar(name, defVal) {
"use strict";
2019-06-24 11:26:25 +00:00
const re = new RegExp('.*[&#]' + name + '=([^&]*)'),
match = document.location.hash.match(re);
if (typeof defVal === 'undefined') { defVal = null; }
2019-06-24 11:26:25 +00:00
if (match) {
return decodeURIComponent(match[1]);
}
2019-06-24 11:26:25 +00:00
return defVal;
}
// 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) {
"use strict";
2019-06-24 11:26:25 +00:00
const val = getHashVar(name);
if (val === null) {
2019-06-24 11:26:25 +00:00
return getQueryVar(name, defVal);
}
2019-06-24 11:26:25 +00:00
return val;
2019-06-24 11:26:25 +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) {
"use strict";
2019-06-24 11:26:25 +00:00
let date, expires;
if (days) {
date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
2019-06-24 11:26:25 +00:00
let secure;
if (document.location.protocol === "https:") {
secure = "; secure";
} else {
secure = "";
}
document.cookie = name + "=" + value + expires + "; path=/" + secure;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
export function readCookie(name, defaultValue) {
"use strict";
2019-06-24 11:26:25 +00:00
const nameEQ = name + "=";
const ca = document.cookie.split(';');
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);
}
}
2019-06-24 11:26:25 +00:00
return (typeof defaultValue !== 'undefined') ? defaultValue : null;
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
export function eraseCookie(name) {
"use strict";
createCookie(name, "", -1);
2019-06-24 11:26:25 +00:00
}
/*
* Setting handling.
*/
2019-06-24 11:26:25 +00:00
let settings = {};
2019-06-24 11:26:25 +00:00
export function initSettings() {
if (!window.chrome || !window.chrome.storage) {
settings = {};
return Promise.resolve();
}
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;
}
// No days means only for this browser session
2019-06-24 11:26:25 +00:00
export function writeSetting(name, value) {
"use strict";
2019-06-24 11:26:25 +00:00
if (settings[name] === value) return;
settings[name] = value;
if (window.chrome && window.chrome.storage) {
2019-06-24 11:26:25 +00:00
window.chrome.storage.sync.set(settings);
} else {
localStorage.setItem(name, value);
}
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
export function readSetting(name, defaultValue) {
"use strict";
2019-06-24 11:26:25 +00:00
let value;
if ((name in settings) || (window.chrome && window.chrome.storage)) {
value = settings[name];
} else {
value = localStorage.getItem(name);
2019-06-24 11:26:25 +00:00
settings[name] = value;
}
if (typeof value === "undefined") {
value = null;
}
2019-06-24 11:26:25 +00:00
if (value === null && typeof defaultValue !== "undefined") {
return defaultValue;
}
2019-06-24 11:26:25 +00:00
return value;
}
export function eraseSetting(name) {
"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];
if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.remove(name);
} else {
localStorage.removeItem(name);
}
2019-06-24 11:26:25 +00:00
}
2019-06-24 11:26:25 +00:00
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;
2019-06-24 11:26:25 +00:00
const elem = document.createElement('a');
elem.href = path;
2020-08-11 12:05:09 +00:00
const paramEq = encodeURIComponent(param) + "=";
2019-06-24 11:26:25 +00:00
let query;
if (elem.search) {
query = elem.search.slice(1).split('&');
} else {
query = [];
}
2020-08-11 12:05:09 +00:00
if (!query.some(v => v.startsWith(paramEq))) {
query.push(paramEq + 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;
}
// 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));
}
2019-06-24 11:26:25 +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));
2019-06-24 11:26:25 +00:00
req.ontimeout = evt => reject(new Error("XHR timed out while trying to load '" + path + "'"));
2019-06-24 11:26:25 +00:00
req.send();
});
}