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.
9 import { initLogging as mainInitLogging
} from '../core/util/logging.js';
11 // init log level reading the logging HTTP param
12 export function initLogging(level
) {
14 if (typeof level
!== "undefined") {
15 mainInitLogging(level
);
17 const param
= document
.location
.href
.match(/logging=([A-Za-z0-9._-]*)/);
18 mainInitLogging(param
|| undefined);
22 // Read a query string variable
23 export function getQueryVar(name
, defVal
) {
25 const re
= new RegExp('.*[?&]' + name
+ '=([^&#]*)'),
26 match
= document
.location
.href
.match(re
);
27 if (typeof defVal
=== 'undefined') { defVal
= null; }
30 return decodeURIComponent(match
[1]);
36 // Read a hash fragment variable
37 export function getHashVar(name
, defVal
) {
39 const re
= new RegExp('.*[&#]' + name
+ '=([^&]*)'),
40 match
= document
.location
.hash
.match(re
);
41 if (typeof defVal
=== 'undefined') { defVal
= null; }
44 return decodeURIComponent(match
[1]);
50 // Read a variable from the fragment or the query string
51 // Fragment takes precedence
52 export function getConfigVar(name
, defVal
) {
54 const val
= getHashVar(name
);
57 return getQueryVar(name
, defVal
);
64 * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
67 // No days means only for this browser session
68 export function createCookie(name
, value
, days
) {
73 date
.setTime(date
.getTime() + (days
* 24 * 60 * 60 * 1000));
74 expires
= "; expires=" + date
.toGMTString();
80 if (document
.location
.protocol
=== "https:") {
85 document
.cookie
= name
+ "=" + value
+ expires
+ "; path=/" + secure
;
88 export function readCookie(name
, defaultValue
) {
90 const nameEQ
= name
+ "=";
91 const ca
= document
.cookie
.split(';');
93 for (let i
= 0; i
< ca
.length
; i
+= 1) {
95 while (c
.charAt(0) === ' ') {
96 c
= c
.substring(1, c
.length
);
98 if (c
.indexOf(nameEQ
) === 0) {
99 return c
.substring(nameEQ
.length
, c
.length
);
103 return (typeof defaultValue
!== 'undefined') ? defaultValue
: null;
106 export function eraseCookie(name
) {
108 createCookie(name
, "", -1);
117 export function initSettings() {
118 if (!window
.chrome
|| !window
.chrome
.storage
) {
120 return Promise
.resolve();
123 return new Promise(resolve
=> window
.chrome
.storage
.sync
.get(resolve
))
124 .then((cfg
) => { settings
= cfg
; });
127 // Update the settings cache, but do not write to permanent storage
128 export function setSetting(name
, value
) {
129 settings
[name
] = value
;
132 // No days means only for this browser session
133 export function writeSetting(name
, value
) {
135 if (settings
[name
] === value
) return;
136 settings
[name
] = value
;
137 if (window
.chrome
&& window
.chrome
.storage
) {
138 window
.chrome
.storage
.sync
.set(settings
);
140 localStorage
.setItem(name
, value
);
144 export function readSetting(name
, defaultValue
) {
147 if ((name
in settings
) || (window
.chrome
&& window
.chrome
.storage
)) {
148 value
= settings
[name
];
150 value
= localStorage
.getItem(name
);
151 settings
[name
] = value
;
153 if (typeof value
=== "undefined") {
157 if (value
=== null && typeof defaultValue
!== "undefined") {
164 export function eraseSetting(name
) {
166 // Deleting here means that next time the setting is read when using local
167 // storage, it will be pulled from local storage again.
168 // If the setting in local storage is changed (e.g. in another tab)
169 // between this delete and the next read, it could lead to an unexpected
171 delete settings
[name
];
172 if (window
.chrome
&& window
.chrome
.storage
) {
173 window
.chrome
.storage
.sync
.remove(name
);
175 localStorage
.removeItem(name
);