Home | History | Annotate | Download | only in include
      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 
     10 /*jslint bitwise: false, white: false, browser: true, devel: true */
     11 /*global Util, window, document */
     12 
     13 // Globals defined here
     14 var WebUtil = {}, $D;
     15 
     16 /*
     17  * Simple DOM selector by ID
     18  */
     19 if (!window.$D) {
     20     window.$D = function (id) {
     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
     40 WebUtil.init_logging = function (level) {
     41     "use strict";
     42     if (typeof level !== "undefined") {
     43         Util._log_level = level;
     44     } else {
     45         var param = document.location.href.match(/logging=([A-Za-z0-9\._\-]*)/);
     46         Util._log_level = (param || ['', Util._log_level])[1];
     47     }
     48     Util.init_logging();
     49 };
     50 
     51 
     52 WebUtil.dirObj = function (obj, depth, parent) {
     53     "use strict";
     54     if (! depth) { depth = 2; }
     55     if (! parent) { parent = ""; }
     56 
     57     // Print the properties of the passed-in object
     58     var msg = "";
     59     for (var i in obj) {
     60         if ((depth > 1) && (typeof obj[i] === "object")) {
     61             // Recurse attributes that are objects
     62             msg += WebUtil.dirObj(obj[i], depth - 1, parent + "." + i);
     63         } else {
     64             //val = new String(obj[i]).replace("\n", " ");
     65             var val = "";
     66             if (typeof(obj[i]) === "undefined") {
     67                 val = "undefined";
     68             } else {
     69                 val = obj[i].toString().replace("\n", " ");
     70             }
     71             if (val.length > 30) {
     72                 val = val.substr(0, 30) + "...";
     73             }
     74             msg += parent + "." + i + ": " + val + "\n";
     75         }
     76     }
     77     return msg;
     78 };
     79 
     80 // Read a query string variable
     81 WebUtil.getQueryVar = function (name, defVal) {
     82     "use strict";
     83     var re = new RegExp('.*[?&]' + name + '=([^&#]*)'),
     84         match = document.location.href.match(re);
     85     if (typeof defVal === 'undefined') { defVal = null; }
     86     if (match) {
     87         return decodeURIComponent(match[1]);
     88     } else {
     89         return defVal;
     90     }
     91 };
     92 
     93 
     94 /*
     95  * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
     96  */
     97 
     98 // No days means only for this browser session
     99 WebUtil.createCookie = function (name, value, days) {
    100     "use strict";
    101     var date, expires;
    102     if (days) {
    103         date = new Date();
    104         date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
    105         expires = "; expires=" + date.toGMTString();
    106     } else {
    107         expires = "";
    108     }
    109 
    110     var secure;
    111     if (document.location.protocol === "https:") {
    112         secure = "; secure";
    113     } else {
    114         secure = "";
    115     }
    116     document.cookie = name + "=" + value + expires + "; path=/" + secure;
    117 };
    118 
    119 WebUtil.readCookie = function (name, defaultValue) {
    120     "use strict";
    121     var nameEQ = name + "=",
    122         ca = document.cookie.split(';');
    123 
    124     for (var i = 0; i < ca.length; i += 1) {
    125         var c = ca[i];
    126         while (c.charAt(0) === ' ') { c = c.substring(1, c.length); }
    127         if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
    128     }
    129     return (typeof defaultValue !== 'undefined') ? defaultValue : null;
    130 };
    131 
    132 WebUtil.eraseCookie = function (name) {
    133     "use strict";
    134     WebUtil.createCookie(name, "", -1);
    135 };
    136 
    137 /*
    138  * Setting handling.
    139  */
    140 
    141 WebUtil.initSettings = function (callback /*, ...callbackArgs */) {
    142     "use strict";
    143     var callbackArgs = Array.prototype.slice.call(arguments, 1);
    144     if (window.chrome && window.chrome.storage) {
    145         window.chrome.storage.sync.get(function (cfg) {
    146             WebUtil.settings = cfg;
    147             console.log(WebUtil.settings);
    148             if (callback) {
    149                 callback.apply(this, callbackArgs);
    150             }
    151         });
    152     } else {
    153         // No-op
    154         if (callback) {
    155             callback.apply(this, callbackArgs);
    156         }
    157     }
    158 };
    159 
    160 // No days means only for this browser session
    161 WebUtil.writeSetting = function (name, value) {
    162     "use strict";
    163     if (window.chrome && window.chrome.storage) {
    164         //console.log("writeSetting:", name, value);
    165         if (WebUtil.settings[name] !== value) {
    166             WebUtil.settings[name] = value;
    167             window.chrome.storage.sync.set(WebUtil.settings);
    168         }
    169     } else {
    170         localStorage.setItem(name, value);
    171     }
    172 };
    173 
    174 WebUtil.readSetting = function (name, defaultValue) {
    175     "use strict";
    176     var value;
    177     if (window.chrome && window.chrome.storage) {
    178         value = WebUtil.settings[name];
    179     } else {
    180         value = localStorage.getItem(name);
    181     }
    182     if (typeof value === "undefined") {
    183         value = null;
    184     }
    185     if (value === null && typeof defaultValue !== undefined) {
    186         return defaultValue;
    187     } else {
    188         return value;
    189     }
    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 /*
    203  * Alternate stylesheet selection
    204  */
    205 WebUtil.getStylesheets = function () {
    206     "use strict";
    207     var links = document.getElementsByTagName("link");
    208     var sheets = [];
    209 
    210     for (var i = 0; i < links.length; i += 1) {
    211         if (links[i].title &&
    212             links[i].rel.toUpperCase().indexOf("STYLESHEET") > -1) {
    213             sheets.push(links[i]);
    214         }
    215     }
    216     return sheets;
    217 };
    218 
    219 // No sheet means try and use value from cookie, null sheet used to
    220 // clear all alternates.
    221 WebUtil.selectStylesheet = function (sheet) {
    222     "use strict";
    223     if (typeof sheet === 'undefined') {
    224         sheet = 'default';
    225     }
    226 
    227     var sheets = WebUtil.getStylesheets();
    228     for (var i = 0; i < sheets.length; i += 1) {
    229         var link = sheets[i];
    230         if (link.title === sheet) {
    231             Util.Debug("Using stylesheet " + sheet);
    232             link.disabled = false;
    233         } else {
    234             //Util.Debug("Skipping stylesheet " + link.title);
    235             link.disabled = true;
    236         }
    237     }
    238     return sheet;
    239 };
    240