summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSamuel Mannehed <samuel@cendio.se>2022-11-02 10:23:36 +0100
committerSamuel Mannehed <samuel@cendio.se>2022-11-02 10:23:36 +0100
commit115f89292e3dc777b6f2b87b1a61a7b57681b6c6 (patch)
tree6bbf13e65767bbb4fe43e8f4334e7779e1bd5c48
parent0ef75824a4a6a9a4873ddb4f9a39050ad7b87565 (diff)
downloadnovnc-localStorage.tar.gz
Don't crash if we can't use localStoragelocalStorage
Our settings are not a fatal requirement, we can fall back on the default values if they can't be accessed. A scenario where we've seen this happen is when cookies are disabled in the browser. It seems localStorage is disabled along with cookies in these settings. Fixes issue #1577.
-rw-r--r--app/webutil.js32
1 files changed, 26 insertions, 6 deletions
diff --git a/app/webutil.js b/app/webutil.js
index d42b7f2..afa099e 100644
--- a/app/webutil.js
+++ b/app/webutil.js
@@ -6,16 +6,16 @@
* See README.md for usage and integration instructions.
*/
-import { initLogging as mainInitLogging } from '../core/util/logging.js';
+import * as Log from '../core/util/logging.js';
// init log level reading the logging HTTP param
export function initLogging(level) {
"use strict";
if (typeof level !== "undefined") {
- mainInitLogging(level);
+ Log.initLogging(level);
} else {
const param = document.location.href.match(/logging=([A-Za-z0-9._-]*)/);
- mainInitLogging(param || undefined);
+ Log.initLogging(param || undefined);
}
}
@@ -146,7 +146,7 @@ export function writeSetting(name, value) {
if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.set(settings);
} else {
- localStorage.setItem(name, value);
+ localStorageWrapper("setItem", [name, value]);
}
}
@@ -156,7 +156,7 @@ export function readSetting(name, defaultValue) {
if ((name in settings) || (window.chrome && window.chrome.storage)) {
value = settings[name];
} else {
- value = localStorage.getItem(name);
+ value = localStorageWrapper("getItem", [name]);
settings[name] = value;
}
if (typeof value === "undefined") {
@@ -181,6 +181,26 @@ export function eraseSetting(name) {
if (window.chrome && window.chrome.storage) {
window.chrome.storage.sync.remove(name);
} else {
- localStorage.removeItem(name);
+ localStorageWrapper("removeItem", [name]);
}
}
+
+let localStorageWarned = false;
+
+function localStorageWrapper(func, args) {
+ let r;
+ try {
+ r = localStorage[func](...args);
+ } catch (e) {
+ if (e instanceof DOMException) {
+ if (!localStorageWarned) {
+ Log.Warn("Couldn't access noVNC settings, are cookies disabled?");
+ localStorageWarned = true;
+ }
+ Log.Debug("'localStorage." + func + "(" + args.join(",") + ")' failed: " + e);
+ } else {
+ throw e;
+ }
+ }
+ return r;
+}