webutil.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2012 Joel Martin
  4. * Licensed under MPL 2.0 (see LICENSE.txt)
  5. *
  6. * See README.md for usage and integration instructions.
  7. */
  8. "use strict";
  9. /*jslint bitwise: false, white: false */
  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. if (typeof level !== "undefined") {
  36. Util._log_level = level;
  37. } else {
  38. Util._log_level = (document.location.href.match(
  39. /logging=([A-Za-z0-9\._\-]*)/) ||
  40. ['', Util._log_level])[1];
  41. }
  42. Util.init_logging();
  43. };
  44. WebUtil.dirObj = function (obj, depth, parent) {
  45. var i, msg = "", val = "";
  46. if (! depth) { depth=2; }
  47. if (! parent) { parent= ""; }
  48. // Print the properties of the passed-in object
  49. for (i in obj) {
  50. if ((depth > 1) && (typeof obj[i] === "object")) {
  51. // Recurse attributes that are objects
  52. msg += WebUtil.dirObj(obj[i], depth-1, parent + "." + i);
  53. } else {
  54. //val = new String(obj[i]).replace("\n", " ");
  55. if (typeof(obj[i]) === "undefined") {
  56. val = "undefined";
  57. } else {
  58. val = obj[i].toString().replace("\n", " ");
  59. }
  60. if (val.length > 30) {
  61. val = val.substr(0,30) + "...";
  62. }
  63. msg += parent + "." + i + ": " + val + "\n";
  64. }
  65. }
  66. return msg;
  67. };
  68. // Read a query string variable
  69. WebUtil.getQueryVar = function(name, defVal) {
  70. var re = new RegExp('[?][^#]*' + name + '=([^&#]*)'),
  71. match = document.location.href.match(re);
  72. if (typeof defVal === 'undefined') { defVal = null; }
  73. if (match) {
  74. return decodeURIComponent(match[1]);
  75. } else {
  76. return defVal;
  77. }
  78. };
  79. /*
  80. * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
  81. */
  82. // No days means only for this browser session
  83. WebUtil.createCookie = function(name,value,days) {
  84. var date, expires;
  85. if (days) {
  86. date = new Date();
  87. date.setTime(date.getTime()+(days*24*60*60*1000));
  88. expires = "; expires="+date.toGMTString();
  89. }
  90. else {
  91. expires = "";
  92. }
  93. document.cookie = name+"="+value+expires+"; path=/";
  94. };
  95. WebUtil.readCookie = function(name, defaultValue) {
  96. var i, c, nameEQ = name + "=", ca = document.cookie.split(';');
  97. for(i=0; i < ca.length; i += 1) {
  98. c = ca[i];
  99. while (c.charAt(0) === ' ') { c = c.substring(1,c.length); }
  100. if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); }
  101. }
  102. return (typeof defaultValue !== 'undefined') ? defaultValue : null;
  103. };
  104. WebUtil.eraseCookie = function(name) {
  105. WebUtil.createCookie(name,"",-1);
  106. };
  107. /*
  108. * Setting handling.
  109. */
  110. WebUtil.initSettings = function(callback) {
  111. var callbackArgs = Array.prototype.slice.call(arguments, 1);
  112. if (window.chrome && window.chrome.storage) {
  113. window.chrome.storage.sync.get(function (cfg) {
  114. WebUtil.settings = cfg;
  115. console.log(WebUtil.settings);
  116. if (callback) {
  117. callback.apply(this, callbackArgs);
  118. }
  119. });
  120. } else {
  121. // No-op
  122. if (callback) {
  123. callback.apply(this, callbackArgs);
  124. }
  125. }
  126. };
  127. // No days means only for this browser session
  128. WebUtil.writeSetting = function(name, value) {
  129. if (window.chrome && window.chrome.storage) {
  130. //console.log("writeSetting:", name, value);
  131. if (WebUtil.settings[name] !== value) {
  132. WebUtil.settings[name] = value;
  133. window.chrome.storage.sync.set(WebUtil.settings);
  134. }
  135. } else {
  136. localStorage.setItem(name, value);
  137. }
  138. };
  139. WebUtil.readSetting = function(name, defaultValue) {
  140. var value;
  141. if (window.chrome && window.chrome.storage) {
  142. value = WebUtil.settings[name];
  143. } else {
  144. value = localStorage.getItem(name);
  145. }
  146. if (typeof value === "undefined") {
  147. value = null;
  148. }
  149. if (value === null && typeof defaultValue !== undefined) {
  150. return defaultValue;
  151. } else {
  152. return value;
  153. }
  154. };
  155. WebUtil.eraseSetting = function(name) {
  156. if (window.chrome && window.chrome.storage) {
  157. window.chrome.storage.sync.remove(name);
  158. delete WebUtil.settings[name];
  159. } else {
  160. localStorage.removeItem(name);
  161. }
  162. };
  163. /*
  164. * Alternate stylesheet selection
  165. */
  166. WebUtil.getStylesheets = function() { var i, links, sheets = [];
  167. links = document.getElementsByTagName("link");
  168. for (i = 0; i < links.length; i += 1) {
  169. if (links[i].title &&
  170. links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
  171. sheets.push(links[i]);
  172. }
  173. }
  174. return sheets;
  175. };
  176. // No sheet means try and use value from cookie, null sheet used to
  177. // clear all alternates.
  178. WebUtil.selectStylesheet = function(sheet) {
  179. var i, link, sheets = WebUtil.getStylesheets();
  180. if (typeof sheet === 'undefined') {
  181. sheet = 'default';
  182. }
  183. for (i=0; i < sheets.length; i += 1) {
  184. link = sheets[i];
  185. if (link.title === sheet) {
  186. Util.Debug("Using stylesheet " + sheet);
  187. link.disabled = false;
  188. } else {
  189. //Util.Debug("Skipping stylesheet " + link.title);
  190. link.disabled = true;
  191. }
  192. }
  193. return sheet;
  194. };