webutil.js 6.5 KB

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