webutil.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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. /*
  83. * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
  84. */
  85. // No days means only for this browser session
  86. WebUtil.createCookie = function (name, value, days) {
  87. "use strict";
  88. var date, expires;
  89. if (days) {
  90. date = new Date();
  91. date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
  92. expires = "; expires=" + date.toGMTString();
  93. } else {
  94. expires = "";
  95. }
  96. var secure;
  97. if (document.location.protocol === "https:") {
  98. secure = "; secure";
  99. } else {
  100. secure = "";
  101. }
  102. document.cookie = name + "=" + value + expires + "; path=/" + secure;
  103. };
  104. WebUtil.readCookie = function (name, defaultValue) {
  105. "use strict";
  106. var nameEQ = name + "=",
  107. ca = document.cookie.split(';');
  108. for (var i = 0; i < ca.length; i += 1) {
  109. var c = ca[i];
  110. while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
  111. if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
  112. }
  113. return (typeof defaultValue !== 'undefined') ? defaultValue : null;
  114. };
  115. WebUtil.eraseCookie = function (name) {
  116. "use strict";
  117. WebUtil.createCookie(name, "", -1);
  118. };
  119. /*
  120. * Setting handling.
  121. */
  122. WebUtil.initSettings = function (callback /*, ...callbackArgs */) {
  123. "use strict";
  124. var callbackArgs = Array.prototype.slice.call(arguments, 1);
  125. if (window.chrome && window.chrome.storage) {
  126. window.chrome.storage.sync.get(function (cfg) {
  127. WebUtil.settings = cfg;
  128. console.log(WebUtil.settings);
  129. if (callback) {
  130. callback.apply(this, callbackArgs);
  131. }
  132. });
  133. } else {
  134. // No-op
  135. if (callback) {
  136. callback.apply(this, callbackArgs);
  137. }
  138. }
  139. };
  140. // No days means only for this browser session
  141. WebUtil.writeSetting = function (name, value) {
  142. "use strict";
  143. if (window.chrome && window.chrome.storage) {
  144. //console.log("writeSetting:", name, value);
  145. if (WebUtil.settings[name] !== value) {
  146. WebUtil.settings[name] = value;
  147. window.chrome.storage.sync.set(WebUtil.settings);
  148. }
  149. } else {
  150. localStorage.setItem(name, value);
  151. }
  152. };
  153. WebUtil.readSetting = function (name, defaultValue) {
  154. "use strict";
  155. var value;
  156. if (window.chrome && window.chrome.storage) {
  157. value = WebUtil.settings[name];
  158. } else {
  159. value = localStorage.getItem(name);
  160. }
  161. if (typeof value === "undefined") {
  162. value = null;
  163. }
  164. if (value === null && typeof defaultValue !== undefined) {
  165. return defaultValue;
  166. } else {
  167. return value;
  168. }
  169. };
  170. WebUtil.eraseSetting = function (name) {
  171. "use strict";
  172. if (window.chrome && window.chrome.storage) {
  173. window.chrome.storage.sync.remove(name);
  174. delete WebUtil.settings[name];
  175. } else {
  176. localStorage.removeItem(name);
  177. }
  178. };
  179. /*
  180. * Alternate stylesheet selection
  181. */
  182. WebUtil.getStylesheets = function () {
  183. "use strict";
  184. var links = document.getElementsByTagName("link");
  185. var sheets = [];
  186. for (var i = 0; i < links.length; i += 1) {
  187. if (links[i].title &&
  188. links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
  189. sheets.push(links[i]);
  190. }
  191. }
  192. return sheets;
  193. };
  194. // No sheet means try and use value from cookie, null sheet used to
  195. // clear all alternates.
  196. WebUtil.selectStylesheet = function (sheet) {
  197. "use strict";
  198. if (typeof sheet === 'undefined') {
  199. sheet = 'default';
  200. }
  201. var sheets = WebUtil.getStylesheets();
  202. for (var i = 0; i < sheets.length; i += 1) {
  203. var link = sheets[i];
  204. if (link.title === sheet) {
  205. Util.Debug("Using stylesheet " + sheet);
  206. link.disabled = false;
  207. } else {
  208. //Util.Debug("Skipping stylesheet " + link.title);
  209. link.disabled = true;
  210. }
  211. }
  212. return sheet;
  213. };