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