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