webutil.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * noVNC: HTML5 VNC client
  3. * Copyright (C) 2010 Joel Martin
  4. * Licensed under LGPL-3 (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 window, document */
  11. // Globals defined here
  12. var WebUtil = {}, $D;
  13. /*
  14. * Simple DOM selector by ID
  15. */
  16. if (!window.$D) {
  17. $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() {
  35. Util._log_level = (document.location.href.match(
  36. /logging=([A-Za-z0-9\._\-]*)/) ||
  37. ['', Util._log_level])[1];
  38. Util.init_logging()
  39. }
  40. WebUtil.init_logging();
  41. WebUtil.dirObj = function (obj, depth, parent) {
  42. var i, msg = "", val = "";
  43. if (! depth) { depth=2; }
  44. if (! parent) { parent= ""; }
  45. // Print the properties of the passed-in object
  46. for (i in obj) {
  47. if ((depth > 1) && (typeof obj[i] === "object")) {
  48. // Recurse attributes that are objects
  49. msg += WebUtil.dirObj(obj[i], depth-1, parent + "." + i);
  50. } else {
  51. //val = new String(obj[i]).replace("\n", " ");
  52. val = obj[i].toString().replace("\n", " ");
  53. if (val.length > 30) {
  54. val = val.substr(0,30) + "...";
  55. }
  56. msg += parent + "." + i + ": " + val + "\n";
  57. }
  58. }
  59. return msg;
  60. };
  61. // Read a query string variable
  62. WebUtil.getQueryVar = function(name, defVal) {
  63. var re = new RegExp('[?][^#]*' + name + '=([^&#]*)');
  64. if (typeof defVal === 'undefined') { defVal = null; }
  65. return (document.location.href.match(re) || ['',defVal])[1];
  66. };
  67. /*
  68. * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
  69. */
  70. // No days means only for this browser session
  71. WebUtil.createCookie = function(name,value,days) {
  72. var date, expires;
  73. if (days) {
  74. date = new Date();
  75. date.setTime(date.getTime()+(days*24*60*60*1000));
  76. expires = "; expires="+date.toGMTString();
  77. }
  78. else {
  79. expires = "";
  80. }
  81. document.cookie = name+"="+value+expires+"; path=/";
  82. };
  83. WebUtil.readCookie = function(name, defaultValue) {
  84. var i, c, nameEQ = name + "=", ca = document.cookie.split(';');
  85. for(i=0; i < ca.length; i += 1) {
  86. c = ca[i];
  87. while (c.charAt(0) === ' ') { c = c.substring(1,c.length); }
  88. if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length,c.length); }
  89. }
  90. return (typeof defaultValue !== 'undefined') ? defaultValue : null;
  91. };
  92. WebUtil.eraseCookie = function(name) {
  93. WebUtil.createCookie(name,"",-1);
  94. };
  95. /*
  96. * Alternate stylesheet selection
  97. */
  98. WebUtil.getStylesheets = function() { var i, links, sheets = [];
  99. links = document.getElementsByTagName("link");
  100. for (i = 0; i < links.length; i += 1) {
  101. if (links[i].title &&
  102. links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
  103. sheets.push(links[i]);
  104. }
  105. }
  106. return sheets;
  107. };
  108. // No sheet means try and use value from cookie, null sheet used to
  109. // clear all alternates.
  110. WebUtil.selectStylesheet = function(sheet) {
  111. var i, link, sheets = WebUtil.getStylesheets();
  112. if (typeof sheet === 'undefined') {
  113. sheet = 'default';
  114. }
  115. for (i=0; i < sheets.length; i += 1) {
  116. link = sheets[i];
  117. if (link.title === sheet) {
  118. Util.Debug("Using stylesheet " + sheet);
  119. link.disabled = false;
  120. } else {
  121. //Util.Debug("Skipping stylesheet " + link.title);
  122. link.disabled = true;
  123. }
  124. }
  125. return sheet;
  126. };