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)
7 * See README.md for usage and integration instructions.
10 /*jslint bitwise: false, white: false, browser: true, devel: true */
11 /*global Util, window, document */
13 import { init_logging as main_init_logging
} from '../core/util/logging.js';
15 // init log level reading the logging HTTP param
16 export function init_logging (level
) {
18 if (typeof level
!== "undefined") {
19 main_init_logging(level
);
21 var param
= document
.location
.href
.match(/logging=([A-Za-z0-9\._\-]*)/);
22 main_init_logging(param
|| undefined);
26 // Read a query string variable
27 export function getQueryVar (name
, defVal
) {
29 var re
= new RegExp('.*[?&]' + name
+ '=([^&#]*)'),
30 match
= document
.location
.href
.match(re
);
31 if (typeof defVal
=== 'undefined') { defVal
= null; }
33 return decodeURIComponent(match
[1]);
39 // Read a hash fragment variable
40 export function getHashVar (name
, defVal
) {
42 var re
= new RegExp('.*[&#]' + name
+ '=([^&]*)'),
43 match
= document
.location
.hash
.match(re
);
44 if (typeof defVal
=== 'undefined') { defVal
= null; }
46 return decodeURIComponent(match
[1]);
52 // Read a variable from the fragment or the query string
53 // Fragment takes precedence
54 export function getConfigVar (name
, defVal
) {
56 var val
= getHashVar(name
);
58 val
= 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 var nameEQ
= name
+ "=",
91 ca
= document
.cookie
.split(';');
93 for (var i
= 0; i
< ca
.length
; i
+= 1) {
95 while (c
.charAt(0) === ' ') { c
= c
.substring(1, c
.length
); }
96 if (c
.indexOf(nameEQ
) === 0) { return c
.substring(nameEQ
.length
, c
.length
); }
98 return (typeof defaultValue
!== 'undefined') ? defaultValue
: null;
101 export function eraseCookie (name
) {
103 createCookie(name
, "", -1);
112 export function initSettings (callback
/*, ...callbackArgs */) {
114 var callbackArgs
= Array
.prototype.slice
.call(arguments
, 1);
115 if (window
.chrome
&& window
.chrome
.storage
) {
116 window
.chrome
.storage
.sync
.get(function (cfg
) {
118 console
.log(settings
);
120 callback
.apply(this, callbackArgs
);
126 callback
.apply(this, callbackArgs
);
131 // No days means only for this browser session
132 export function writeSetting (name
, value
) {
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
);
141 localStorage
.setItem(name
, value
);
145 export function readSetting (name
, defaultValue
) {
148 if (window
.chrome
&& window
.chrome
.storage
) {
149 value
= settings
[name
];
151 value
= localStorage
.getItem(name
);
153 if (typeof value
=== "undefined") {
156 if (value
=== null && typeof defaultValue
!== "undefined") {
163 export function eraseSetting (name
) {
165 if (window
.chrome
&& window
.chrome
.storage
) {
166 window
.chrome
.storage
.sync
.remove(name
);
167 delete settings
[name
];
169 localStorage
.removeItem(name
);
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)
178 var elem
= document
.createElement('a');
181 var param_eq
= encodeURIComponent(param
) + "=";
184 query
= elem
.search
.slice(1).split('&');
189 if (!query
.some(function (v
) { return v
.startsWith(param_eq
); })) {
190 query
.push(param_eq
+ encodeURIComponent(value
));
191 elem
.search
= "?" + query
.join("&");
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
;
199 return elem
.pathname
+ elem
.search
+ elem
.hash
;
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
);
212 req
.onload = function () {
213 if (req
.status
=== 200) {
215 var resObj
= JSON
.parse(req
.responseText
);
222 reject(new Error("XHR got non-200 status while trying to load '" + path
+ "': " + req
.status
));
226 req
.onerror = function (evt
) {
227 reject(new Error("XHR encountered an error while trying to load '" + path
+ "': " + evt
.message
));
230 req
.ontimeout = function (evt
) {
231 reject(new Error("XHR timed out while trying to load '" + path
+ "'"));