]> git.proxmox.com Git - mirror_novnc.git/blob - app/webutil.js
Add eslint and fix reported issues
[mirror_novnc.git] / app / webutil.js
1 /*
2 * noVNC: HTML5 VNC client
3 * Copyright (C) 2012 Joel Martin
4 * Copyright (C) 2013 NTT corp.
5 * Licensed under MPL 2.0 (see LICENSE.txt)
6 *
7 * See README.md for usage and integration instructions.
8 */
9
10 import { init_logging as main_init_logging } from '../core/util/logging.js';
11
12 // init log level reading the logging HTTP param
13 export function init_logging (level) {
14 "use strict";
15 if (typeof level !== "undefined") {
16 main_init_logging(level);
17 } else {
18 var param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
19 main_init_logging(param || undefined);
20 }
21 }
22
23 // Read a query string variable
24 export function getQueryVar (name, defVal) {
25 "use strict";
26 var re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
27 match = document.location.href.match(re);
28 if (typeof defVal === 'undefined') { defVal = null; }
29 if (match) {
30 return decodeURIComponent(match[1]);
31 } else {
32 return defVal;
33 }
34 }
35
36 // Read a hash fragment variable
37 export function getHashVar (name, defVal) {
38 "use strict";
39 var re = new RegExp('.*[&#]' + name + '=([^&]*)'),
40 match = document.location.hash.match(re);
41 if (typeof defVal === 'undefined') { defVal = null; }
42 if (match) {
43 return decodeURIComponent(match[1]);
44 } else {
45 return defVal;
46 }
47 }
48
49 // Read a variable from the fragment or the query string
50 // Fragment takes precedence
51 export function getConfigVar (name, defVal) {
52 "use strict";
53 var val = getHashVar(name);
54 if (val === null) {
55 val = getQueryVar(name, defVal);
56 }
57 return val;
58 }
59
60 /*
61 * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
62 */
63
64 // No days means only for this browser session
65 export function createCookie (name, value, days) {
66 "use strict";
67 var date, expires;
68 if (days) {
69 date = new Date();
70 date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
71 expires = "; expires=" + date.toGMTString();
72 } else {
73 expires = "";
74 }
75
76 var secure;
77 if (document.location.protocol === "https:") {
78 secure = "; secure";
79 } else {
80 secure = "";
81 }
82 document.cookie = name + "=" + value + expires + "; path=/" + secure;
83 }
84
85 export function readCookie (name, defaultValue) {
86 "use strict";
87 var nameEQ = name + "=",
88 ca = document.cookie.split(';');
89
90 for (var i = 0; i < ca.length; i += 1) {
91 var c = ca[i];
92 while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
93 if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
94 }
95 return (typeof defaultValue !== 'undefined') ? defaultValue : null;
96 }
97
98 export function eraseCookie (name) {
99 "use strict";
100 createCookie(name, "", -1);
101 }
102
103 /*
104 * Setting handling.
105 */
106
107 var settings = {};
108
109 export function initSettings (callback /*, ...callbackArgs */) {
110 "use strict";
111 var callbackArgs = Array.prototype.slice.call(arguments, 1);
112 if (window.chrome && window.chrome.storage) {
113 window.chrome.storage.sync.get(function (cfg) {
114 settings = cfg;
115 if (callback) {
116 callback.apply(this, callbackArgs);
117 }
118 });
119 } else {
120 settings = {};
121 if (callback) {
122 callback.apply(this, callbackArgs);
123 }
124 }
125 }
126
127 // Update the settings cache, but do not write to permanent storage
128 export function setSetting (name, value) {
129 settings[name] = value;
130 }
131
132 // No days means only for this browser session
133 export function writeSetting (name, value) {
134 "use strict";
135 if (settings[name] === value) return;
136 settings[name] = value;
137 if (window.chrome && window.chrome.storage) {
138 window.chrome.storage.sync.set(settings);
139 } else {
140 localStorage.setItem(name, value);
141 }
142 }
143
144 export function readSetting (name, defaultValue) {
145 "use strict";
146 var value;
147 if ((name in settings) || (window.chrome && window.chrome.storage)) {
148 value = settings[name];
149 } else {
150 value = localStorage.getItem(name);
151 settings[name] = value;
152 }
153 if (typeof value === "undefined") {
154 value = null;
155 }
156 if (value === null && typeof defaultValue !== "undefined") {
157 return defaultValue;
158 } else {
159 return value;
160 }
161 }
162
163 export function eraseSetting (name) {
164 "use strict";
165 // Deleting here means that next time the setting is read when using local
166 // storage, it will be pulled from local storage again.
167 // If the setting in local storage is changed (e.g. in another tab)
168 // between this delete and the next read, it could lead to an unexpected
169 // value change.
170 delete settings[name];
171 if (window.chrome && window.chrome.storage) {
172 window.chrome.storage.sync.remove(name);
173 } else {
174 localStorage.removeItem(name);
175 }
176 }
177
178 export function injectParamIfMissing (path, param, value) {
179 // force pretend that we're dealing with a relative path
180 // (assume that we wanted an extra if we pass one in)
181 path = "/" + path;
182
183 var elem = document.createElement('a');
184 elem.href = path;
185
186 var param_eq = encodeURIComponent(param) + "=";
187 var query;
188 if (elem.search) {
189 query = elem.search.slice(1).split('&');
190 } else {
191 query = [];
192 }
193
194 if (!query.some(function (v) { return v.startsWith(param_eq); })) {
195 query.push(param_eq + encodeURIComponent(value));
196 elem.search = "?" + query.join("&");
197 }
198
199 // some browsers (e.g. IE11) may occasionally omit the leading slash
200 // in the elem.pathname string. Handle that case gracefully.
201 if (elem.pathname.charAt(0) == "/") {
202 return elem.pathname.slice(1) + elem.search + elem.hash;
203 } else {
204 return elem.pathname + elem.search + elem.hash;
205 }
206 }
207
208 // sadly, we can't use the Fetch API until we decide to drop
209 // IE11 support or polyfill promises and fetch in IE11.
210 // resolve will receive an object on success, while reject
211 // will receive either an event or an error on failure.
212 export function fetchJSON(path, resolve, reject) {
213 // NB: IE11 doesn't support JSON as a responseType
214 var req = new XMLHttpRequest();
215 req.open('GET', path);
216
217 req.onload = function () {
218 if (req.status === 200) {
219 try {
220 var resObj = JSON.parse(req.responseText);
221 } catch (err) {
222 reject(err);
223 return;
224 }
225 resolve(resObj);
226 } else {
227 reject(new Error("XHR got non-200 status while trying to load '" + path + "': " + req.status));
228 }
229 };
230
231 req.onerror = function (evt) {
232 reject(new Error("XHR encountered an error while trying to load '" + path + "': " + evt.message));
233 };
234
235 req.ontimeout = function (evt) {
236 reject(new Error("XHR timed out while trying to load '" + path + "'"));
237 };
238
239 req.send();
240 }