webutil.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // Globals defined here
  12. var WebUtil = {}, $D;
  13. /*
  14. * Simple DOM selector by ID
  15. */
  16. if (!window.$D) {
  17. window.$D = function (id) {
  18. if (document.getElementById) {
  19. return document.getElementById(id);
  20. } else if (document.all) {
  21. return document.all[id];
  22. } else if (document.layers) {
  23. return document.layers[id];
  24. }
  25. return undefined;
  26. };
  27. }
  28. /*
  29. * ------------------------------------------------------
  30. * Namespaced in WebUtil
  31. * ------------------------------------------------------
  32. */
  33. // init log level reading the logging HTTP param
  34. WebUtil.init_logging = function (level) {
  35. "use strict";
  36. if (typeof level !== "undefined") {
  37. Util._log_level = level;
  38. } else {
  39. var param = document.location.href.match(/logging=([A-Za-z0-9\._\-]*)/);
  40. Util._log_level = (param || ['', Util._log_level])[1];
  41. }
  42. Util.init_logging();
  43. };
  44. WebUtil.dirObj = function (obj, depth, parent) {
  45. "use strict";
  46. if (! depth) { depth = 2; }
  47. if (! parent) { parent = ""; }
  48. // Print the properties of the passed-in object
  49. var msg = "";
  50. for (var i in obj) {
  51. if ((depth > 1) && (typeof obj[i] === "object")) {
  52. // Recurse attributes that are objects
  53. msg += WebUtil.dirObj(obj[i], depth - 1, parent + "." + i);
  54. } else {
  55. //val = new String(obj[i]).replace("\n", " ");
  56. var val = "";
  57. if (typeof(obj[i]) === "undefined") {
  58. val = "undefined";
  59. } else {
  60. val = obj[i].toString().replace("\n", " ");
  61. }
  62. if (val.length > 30) {
  63. val = val.substr(0, 30) + "...";
  64. }
  65. msg += parent + "." + i + ": " + val + "\n";
  66. }
  67. }
  68. return msg;
  69. };
  70. // Read a query string variable
  71. WebUtil.getQueryVar = function (name, defVal) {
  72. "use strict";
  73. var re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
  74. match = document.location.href.match(re);
  75. if (typeof defVal === 'undefined') { defVal = null; }
  76. if (match) {
  77. return decodeURIComponent(match[1]);
  78. } else {
  79. return defVal;
  80. }
  81. };
  82. // Read a hash fragment variable
  83. WebUtil.getHashVar = function (name, defVal) {
  84. "use strict";
  85. var re = new RegExp('.*[&#]' + name + '=([^&]*)'),
  86. match = document.location.hash.match(re);
  87. if (typeof defVal === 'undefined') { defVal = null; }
  88. if (match) {
  89. return decodeURIComponent(match[1]);
  90. } else {
  91. return defVal;
  92. }
  93. };
  94. // Read a variable from the fragment or the query string
  95. // Fragment takes precedence
  96. WebUtil.getConfigVar = function (name, defVal) {
  97. "use strict";
  98. var val = WebUtil.getHashVar(name);
  99. if (val === null) {
  100. val = WebUtil.getQueryVar(name, defVal);
  101. }
  102. return val;
  103. };
  104. /*
  105. * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
  106. */
  107. // No days means only for this browser session
  108. WebUtil.createCookie = function (name, value, days) {
  109. "use strict";
  110. var date, expires;
  111. if (days) {
  112. date = new Date();
  113. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  114. expires = "; expires=" + date.toGMTString();
  115. } else {
  116. expires = "";
  117. }
  118. var secure;
  119. if (document.location.protocol === "https:") {
  120. secure = "; secure";
  121. } else {
  122. secure = "";
  123. }
  124. document.cookie = name + "=" + value + expires + "; path=/" + secure;
  125. };
  126. WebUtil.readCookie = function (name, defaultValue) {
  127. "use strict";
  128. var nameEQ = name + "=",
  129. ca = document.cookie.split(';');
  130. for (var i = 0; i < ca.length; i += 1) {
  131. var c = ca[i];
  132. while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
  133. if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
  134. }
  135. return (typeof defaultValue !== 'undefined') ? defaultValue : null;
  136. };
  137. WebUtil.eraseCookie = function (name) {
  138. "use strict";
  139. WebUtil.createCookie(name, "", -1);
  140. };
  141. /*
  142. * Setting handling.
  143. */
  144. WebUtil.initSettings = function (callback /*, ...callbackArgs */) {
  145. "use strict";
  146. var callbackArgs = Array.prototype.slice.call(arguments, 1);
  147. if (window.chrome && window.chrome.storage) {
  148. window.chrome.storage.sync.get(function (cfg) {
  149. WebUtil.settings = cfg;
  150. console.log(WebUtil.settings);
  151. if (callback) {
  152. callback.apply(this, callbackArgs);
  153. }
  154. });
  155. } else {
  156. // No-op
  157. if (callback) {
  158. callback.apply(this, callbackArgs);
  159. }
  160. }
  161. };
  162. // No days means only for this browser session
  163. WebUtil.writeSetting = function (name, value) {
  164. "use strict";
  165. if (window.chrome && window.chrome.storage) {
  166. //console.log("writeSetting:", name, value);
  167. if (WebUtil.settings[name] !== value) {
  168. WebUtil.settings[name] = value;
  169. window.chrome.storage.sync.set(WebUtil.settings);
  170. }
  171. } else {
  172. localStorage.setItem(name, value);
  173. }
  174. };
  175. WebUtil.readSetting = function (name, defaultValue) {
  176. "use strict";
  177. var value;
  178. if (window.chrome && window.chrome.storage) {
  179. value = WebUtil.settings[name];
  180. } else {
  181. value = localStorage.getItem(name);
  182. }
  183. if (typeof value === "undefined") {
  184. value = null;
  185. }
  186. if (value === null && typeof defaultValue !== undefined) {
  187. return defaultValue;
  188. } else {
  189. return value;
  190. }
  191. };
  192. WebUtil.eraseSetting = function (name) {
  193. "use strict";
  194. if (window.chrome && window.chrome.storage) {
  195. window.chrome.storage.sync.remove(name);
  196. delete WebUtil.settings[name];
  197. } else {
  198. localStorage.removeItem(name);
  199. }
  200. };
  201. /*
  202. * Alternate stylesheet selection
  203. */
  204. WebUtil.getStylesheets = function () {
  205. "use strict";
  206. var links = document.getElementsByTagName("link");
  207. var sheets = [];
  208. for (var i = 0; i < links.length; i += 1) {
  209. if (links[i].title &&
  210. links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
  211. sheets.push(links[i]);
  212. }
  213. }
  214. return sheets;
  215. };
  216. // No sheet means try and use value from cookie, null sheet used to
  217. // clear all alternates.
  218. WebUtil.selectStylesheet = function (sheet) {
  219. "use strict";
  220. if (typeof sheet === 'undefined') {
  221. sheet = 'default';
  222. }
  223. var sheets = WebUtil.getStylesheets();
  224. for (var i = 0; i < sheets.length; i += 1) {
  225. var link = sheets[i];
  226. if (link.title === sheet) {
  227. Util.Debug("Using stylesheet " + sheet);
  228. link.disabled = false;
  229. } else {
  230. //Util.Debug("Skipping stylesheet " + link.title);
  231. link.disabled = true;
  232. }
  233. }
  234. return sheet;
  235. };
  236. WebUtil.injectParamIfMissing = function (path, param, value) {
  237. // force pretend that we're dealing with a relative path
  238. // (assume that we wanted an extra if we pass one in)
  239. path = "/" + path;
  240. var elem = document.createElement('a');
  241. elem.href = path;
  242. var param_eq = encodeURIComponent(param) + "=";
  243. var query;
  244. if (elem.search) {
  245. query = elem.search.slice(1).split('&');
  246. } else {
  247. query = [];
  248. }
  249. if (!query.some(function (v) { return v.startsWith(param_eq); })) {
  250. query.push(param_eq + encodeURIComponent(value));
  251. elem.search = "?" + query.join("&");
  252. }
  253. // some browsers (e.g. IE11) may occasionally omit the leading slash
  254. // in the elem.pathname string. Handle that case gracefully.
  255. if (elem.pathname.charAt(0) == "/") {
  256. return elem.pathname.slice(1) + elem.search + elem.hash;
  257. } else {
  258. return elem.pathname + elem.search + elem.hash;
  259. }
  260. };