]> git.proxmox.com Git - mirror_novnc.git/blobdiff - app/webutil.js
Add eslint and fix reported issues
[mirror_novnc.git] / app / webutil.js
index 249a1382fe87fb150562835314bcfbe633864510..73d24af043b5487ecb594272e22d69ffe6370ae1 100644 (file)
@@ -15,10 +15,10 @@ export function init_logging (level) {
     if (typeof level !== "undefined") {
         main_init_logging(level);
     } else {
-        var param = document.location.href.match(/logging=([A-Za-z0-9\._\-]*)/);
+        var param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
         main_init_logging(param || undefined);
     }
-};
+}
 
 // Read a query string variable
 export function getQueryVar (name, defVal) {
@@ -31,7 +31,7 @@ export function getQueryVar (name, defVal) {
     } else {
         return defVal;
     }
-};
+}
 
 // Read a hash fragment variable
 export function getHashVar (name, defVal) {
@@ -44,7 +44,7 @@ export function getHashVar (name, defVal) {
     } else {
         return defVal;
     }
-};
+}
 
 // Read a variable from the fragment or the query string
 // Fragment takes precedence
@@ -55,7 +55,7 @@ export function getConfigVar (name, defVal) {
         val = getQueryVar(name, defVal);
     }
     return val;
-};
+}
 
 /*
  * Cookie handling. Dervied from: http://www.quirksmode.org/js/cookies.html
@@ -80,7 +80,7 @@ export function createCookie (name, value, days) {
         secure = "";
     }
     document.cookie = name + "=" + value + expires + "; path=/" + secure;
-};
+}
 
 export function readCookie (name, defaultValue) {
     "use strict";
@@ -93,12 +93,12 @@ export function readCookie (name, defaultValue) {
         if (c.indexOf(nameEQ) === 0) { return c.substring(nameEQ.length, c.length); }
     }
     return (typeof defaultValue !== 'undefined') ? defaultValue : null;
-};
+}
 
 export function eraseCookie (name) {
     "use strict";
     createCookie(name, "", -1);
-};
+}
 
 /*
  * Setting handling.
@@ -117,33 +117,38 @@ export function initSettings (callback /*, ...callbackArgs */) {
             }
         });
     } else {
-        // No-op
+        settings = {};
         if (callback) {
             callback.apply(this, callbackArgs);
         }
     }
-};
+}
+
+// Update the settings cache, but do not write to permanent storage
+export function setSetting (name, value) {
+    settings[name] = value;
+}
 
 // No days means only for this browser session
 export function writeSetting (name, value) {
     "use strict";
+    if (settings[name] === value) return;
+    settings[name] = value;
     if (window.chrome && window.chrome.storage) {
-        if (settings[name] !== value) {
-            settings[name] = value;
-            window.chrome.storage.sync.set(settings);
-        }
+        window.chrome.storage.sync.set(settings);
     } else {
         localStorage.setItem(name, value);
     }
-};
+}
 
 export function readSetting (name, defaultValue) {
     "use strict";
     var value;
-    if (window.chrome && window.chrome.storage) {
+    if ((name in settings) || (window.chrome && window.chrome.storage)) {
         value = settings[name];
     } else {
         value = localStorage.getItem(name);
+        settings[name] = value;
     }
     if (typeof value === "undefined") {
         value = null;
@@ -153,17 +158,22 @@ export function readSetting (name, defaultValue) {
     } else {
         return value;
     }
-};
+}
 
 export function eraseSetting (name) {
     "use strict";
+    // Deleting here means that next time the setting is read when using local
+    // storage, it will be pulled from local storage again.
+    // If the setting in local storage is changed (e.g. in another tab)
+    // between this delete and the next read, it could lead to an unexpected
+    // value change.
+    delete settings[name];
     if (window.chrome && window.chrome.storage) {
         window.chrome.storage.sync.remove(name);
-        delete settings[name];
     } else {
         localStorage.removeItem(name);
     }
-};
+}
 
 export function injectParamIfMissing (path, param, value) {
     // force pretend that we're dealing with a relative path
@@ -193,7 +203,7 @@ export function injectParamIfMissing (path, param, value) {
     } else {
         return elem.pathname + elem.search + elem.hash;
     }
-};
+}
 
 // sadly, we can't use the Fetch API until we decide to drop
 // IE11 support or polyfill promises and fetch in IE11.