From b32d65207b31b20b1dbdfc0d41fe0e884f0b6e71 Mon Sep 17 00:00:00 2001 From: ed Date: Wed, 24 Jan 2024 02:24:27 +0000 Subject: [PATCH] fix js-error on older chromes in incognito mode; window.localStorage was null, so trying to read would fail seen on falkon 23.08.4 with qtwebengine 5.15.12 (fedora39) might as well be paranoid about the other failure modes too (sudden exceptions on reads and/or writes) --- copyparty/web/browser.html | 3 ++- copyparty/web/browser.js | 4 ++-- copyparty/web/md.html | 7 +++---- copyparty/web/md2.js | 2 +- copyparty/web/mde.html | 6 +++--- copyparty/web/splash.html | 3 ++- copyparty/web/svcs.html | 3 ++- copyparty/web/up2k.js | 4 ++-- copyparty/web/util.js | 27 +++++++++++++++++++++++---- 9 files changed, 40 insertions(+), 19 deletions(-) diff --git a/copyparty/web/browser.html b/copyparty/web/browser.html index 10a82474c..a9e817100 100644 --- a/copyparty/web/browser.html +++ b/copyparty/web/browser.html @@ -148,7 +148,8 @@ logues = {{ logues|tojson if sb_lg else "[]" }}, ls0 = {{ ls0|tojson }}; - document.documentElement.className = localStorage.cpp_thm || dtheme; + var STG = window.localStorage; + document.documentElement.className = (STG && STG.cpp_thm) || dtheme; diff --git a/copyparty/web/browser.js b/copyparty/web/browser.js index 28cf5fa39..8ecc76481 100644 --- a/copyparty/web/browser.js +++ b/copyparty/web/browser.js @@ -3405,8 +3405,8 @@ function sortfiles(nodes) { } catch (ex) { console.log("failed to apply sort config: " + ex); - console.log("resetting fsort " + sread('fsort')) - localStorage.removeItem('fsort'); + console.log("resetting fsort " + sread('fsort')); + sdrop('fsort'); } return nodes; } diff --git a/copyparty/web/md.html b/copyparty/web/md.html index 5e08d07ab..cbc2eb9cd 100644 --- a/copyparty/web/md.html +++ b/copyparty/web/md.html @@ -139,16 +139,15 @@ var md_opt = { }; (function () { - var l = localStorage, - drk = l.light != 1, + var l = window.localStorage, + drk = (l && l.light) != 1, btn = document.getElementById("lightswitch"), f = function (e) { if (e) { e.preventDefault(); drk = !drk; } document.documentElement.className = drk? "z":"y"; btn.innerHTML = "go " + (drk ? "light":"dark"); -l.light = drk? 0:1; +try { l.light = drk? 0:1; } catch (ex) { } }; - btn.onclick = f; f(); })(); diff --git a/copyparty/web/md2.js b/copyparty/web/md2.js index f2f8a01a4..dc2f702b2 100644 --- a/copyparty/web/md2.js +++ b/copyparty/web/md2.js @@ -933,7 +933,7 @@ var set_lno = (function () { var keydown = function (ev) { if (!ev && window.event) { ev = window.event; - if (localStorage.dev_fbw == 1) { + if (dev_fbw == 1) { toast.warn(10, 'hello from fallback code ;_;\ncheck console trace'); console.error('using window.event'); } diff --git a/copyparty/web/mde.html b/copyparty/web/mde.html index bba785142..8b57336bd 100644 --- a/copyparty/web/mde.html +++ b/copyparty/web/mde.html @@ -37,12 +37,12 @@ var md_opt = { }; var lightswitch = (function () { - var l = localStorage, - drk = l.light != 1, + var l = window.localStorage, + drk = (l && l.light) != 1, f = function (e) { if (e) drk = !drk; document.documentElement.className = drk? "z":"y"; -l.light = drk? 0:1; +try { l.light = drk? 0:1; } catch (ex) { } }; f(); return f; diff --git a/copyparty/web/splash.html b/copyparty/web/splash.html index 47db5a1a7..29776e19f 100644 --- a/copyparty/web/splash.html +++ b/copyparty/web/splash.html @@ -110,7 +110,8 @@ var SR = {{ r|tojson }}, lang="{{ lang }}", dfavico="{{ favico }}"; -document.documentElement.className=localStorage.cpp_thm||"{{ this.args.theme }}"; +var STG = window.localStorage; +document.documentElement.className = (STG && STG.cpp_thm) || "{{ this.args.theme }}"; diff --git a/copyparty/web/svcs.html b/copyparty/web/svcs.html index c0df4a246..1353ffe60 100644 --- a/copyparty/web/svcs.html +++ b/copyparty/web/svcs.html @@ -238,7 +238,8 @@ var SR = {{ r|tojson }}, lang="{{ lang }}", dfavico="{{ favico }}"; -document.documentElement.className=localStorage.cpp_thm||"{{ args.theme }}"; +var STG = window.localStorage; +document.documentElement.className = (STG && STG.cpp_thm) || "{{ args.theme }}"; diff --git a/copyparty/web/up2k.js b/copyparty/web/up2k.js index 3148a52a5..51e37d0fe 100644 --- a/copyparty/web/up2k.js +++ b/copyparty/web/up2k.js @@ -431,7 +431,7 @@ function U2pvis(act, btns, uc, st) { if (sread('potato') === null) { btn.click(); toast.inf(30, L.u_gotpot); - localStorage.removeItem('potato'); + sdrop('potato'); } u2f.appendChild(ode); @@ -2687,7 +2687,7 @@ function up2k_init(subtle) { parallel_uploads = v; if (v == u2j) - localStorage.removeItem('nthread'); + sdrop('nthread'); else swrite('nthread', v); diff --git a/copyparty/web/util.js b/copyparty/web/util.js index ca7c08407..dc548cab4 100644 --- a/copyparty/web/util.js +++ b/copyparty/web/util.js @@ -12,6 +12,7 @@ if (window.CGV) var wah = '', + STG = null, NOAC = 'autocorrect="off" autocapitalize="off"', L, tt, treectl, thegrid, up2k, asmCrypto, hashwasm, vbar, marked, T0 = Date.now(), @@ -40,6 +41,16 @@ if (!window.Notification || !Notification.permission) if (!window.FormData) window.FormData = false; +try { + STG = window.localStorage; + STG.STG; +} +catch (ex) { + STG = null; + if ((ex + '').indexOf('sandbox') < 0) + console.log('no localStorage: ' + ex); +} + try { CB = '?' + document.currentScript.src.split('?').pop(); @@ -285,10 +296,11 @@ function anymod(e, shift_ok) { } +var dev_fbw = sread('dev_fbw'); function ev(e) { if (!e && window.event) { e = window.event; - if (localStorage.dev_fbw == 1) { + if (dev_fbw == 1) { toast.warn(10, 'hello from fallback code ;_;\ncheck console trace'); console.error('using window.event'); } @@ -925,9 +937,16 @@ function jcp(obj) { } +function sdrop(key) { + try { + STG.removeItem(key); + } + catch (ex) { } +} + function sread(key, al) { try { - var ret = localStorage.getItem(key); + var ret = STG.getItem(key); return (!al || has(al, ret)) ? ret : null; } catch (e) { @@ -938,9 +957,9 @@ function sread(key, al) { function swrite(key, val) { try { if (val === undefined || val === null) - localStorage.removeItem(key); + STG.removeItem(key); else - localStorage.setItem(key, val); + STG.setItem(key, val); } catch (e) { } }