2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2019 The noVNC Authors
4 * Licensed under MPL 2.0 (see LICENSE.txt)
6 * See README.md for usage and integration instructions.
9import * as Log from '../core/util/logging.js';
11// init log level reading the logging HTTP param
12export function initLogging(level) {
14 if (typeof level !== "undefined") {
15 Log.initLogging(level);
17 const param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
18 Log.initLogging(param || undefined);
22// Read a query string variable
23// A URL with a query parameter can look like this (But will most probably get logged on the http server):
24// https://www.example.com?myqueryparam=myvalue
26// For privacy (Using a hastag #, the parameters will not be sent to the server)
27// the url can be requested in the following way:
28// https://www.example.com#myqueryparam=myvalue&password=secretvalue
30// Even Mixing public and non public parameters will work:
31// https://www.example.com?nonsecretparam=example.com#password=secretvalue
32export function getQueryVar(name, defVal) {
34 const re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
35 match = document.location.href.match(re);
36 if (typeof defVal === 'undefined') { defVal = null; }
39 return decodeURIComponent(match[1]);
45// Read a hash fragment variable
46export function getHashVar(name, defVal) {
48 const re = new RegExp('.*[&#]' + name + '=([^&]*)'),
49 match = document.location.hash.match(re);
50 if (typeof defVal === 'undefined') { defVal = null; }
53 return decodeURIComponent(match[1]);
59// Read a variable from the fragment or the query string
60// Fragment takes precedence
61export function getConfigVar(name, defVal) {
63 const val = getHashVar(name);
66 return getQueryVar(name, defVal);
73 * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
76// No days means only for this browser session
77export function createCookie(name, value, days) {
82 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
83 expires = "; expires=" + date.toGMTString();
89 if (document.location.protocol === "https:") {
94 document.cookie = name + "=" + value + expires + "; path=/" + secure;
97export function readCookie(name, defaultValue) {
99 const nameEQ = name + "=";
100 const ca = document.cookie.split(';');
102 for (let i = 0; i < ca.length; i += 1) {
104 while (c.charAt(0) === ' ') {
105 c = c.substring(1, c.length);
107 if (c.indexOf(nameEQ) === 0) {
108 return c.substring(nameEQ.length, c.length);
112 return (typeof defaultValue !== 'undefined') ? defaultValue : null;
115export function eraseCookie(name) {
117 createCookie(name, "", -1);
126export function initSettings() {
127 if (!window.chrome || !window.chrome.storage) {
129 return Promise.resolve();
132 return new Promise(resolve => window.chrome.storage.sync.get(resolve))
133 .then((cfg) => { settings = cfg; });
136// Update the settings cache, but do not write to permanent storage
137export function setSetting(name, value) {
138 settings[name] = value;
141// No days means only for this browser session
142export function writeSetting(name, value) {
144 if (settings[name] === value) return;
145 settings[name] = value;
146 if (window.chrome && window.chrome.storage) {
147 window.chrome.storage.sync.set(settings);
149 localStorageSet(name, value);
153export function readSetting(name, defaultValue) {
156 if ((name in settings) || (window.chrome && window.chrome.storage)) {
157 value = settings[name];
159 value = localStorageGet(name);
160 settings[name] = value;
162 if (typeof value === "undefined") {
166 if (value === null && typeof defaultValue !== "undefined") {
173export function eraseSetting(name) {
175 // Deleting here means that next time the setting is read when using local
176 // storage, it will be pulled from local storage again.
177 // If the setting in local storage is changed (e.g. in another tab)
178 // between this delete and the next read, it could lead to an unexpected
180 delete settings[name];
181 if (window.chrome && window.chrome.storage) {
182 window.chrome.storage.sync.remove(name);
184 localStorageRemove(name);
189function logOnce(msg, level = "warn") {
190 if (!loggedMsgs.includes(msg)) {
204 loggedMsgs.push(msg);
208let cookiesMsg = "Couldn't access noVNC settings, are cookies disabled?";
210function localStorageGet(name) {
213 r = localStorage.getItem(name);
215 if (e instanceof DOMException) {
217 logOnce("'localStorage.getItem(" + name + ")' failed: " + e,
225function localStorageSet(name, value) {
227 localStorage.setItem(name, value);
229 if (e instanceof DOMException) {
231 logOnce("'localStorage.setItem(" + name + "," + value +
232 ")' failed: " + e, "debug");
238function localStorageRemove(name) {
240 localStorage.removeItem(name);
242 if (e instanceof DOMException) {
244 logOnce("'localStorage.removeItem(" + name + ")' failed: " + e,