From 0e524f3e9ab80a2d281984e3d044998be3439879 Mon Sep 17 00:00:00 2001 From: Nolan Lawson Date: Wed, 5 Dec 2018 21:34:30 -0800 Subject: [PATCH] fix: detect private browsing and safari blocked cookies (#733) * WIP: detect private browsing and safari blocked cookies * just check for indexeddb * just check for indexeddb * change warning text * change text * change text again * change text again fixes #444 --- inline-script.js | 16 +++++++-- package-lock.json | 5 +++ package.json | 1 + routes/_pages/settings/instances/add.html | 22 +++++++++--- routes/_store/LocalStorageStore.js | 4 +-- routes/_utils/safeLocalStorage.js | 6 ++++ routes/_utils/testStorage.js | 42 +++++++++++++++++++++++ routes/_utils/thunk.js | 11 ++++++ templates/2xx.html | 16 +++++++-- 9 files changed, 111 insertions(+), 12 deletions(-) create mode 100644 routes/_utils/safeLocalStorage.js create mode 100644 routes/_utils/testStorage.js create mode 100644 routes/_utils/thunk.js diff --git a/inline-script.js b/inline-script.js index 6dc37c74..6b7c1b4e 100644 --- a/inline-script.js +++ b/inline-script.js @@ -3,7 +3,17 @@ // the build process and write it to inline-script-checksum.json. window.__themeColors = process.env.THEME_COLORS -if (localStorage.store_currentInstance && localStorage.store_instanceThemes) { +const hasLocalStorage = (() => { + try { + // iOS safari throws here if cookies are disabled + let unused = localStorage.length // eslint-disable-line + return true + } catch (e) { + return false + } +})() + +if (hasLocalStorage && localStorage.store_currentInstance && localStorage.store_instanceThemes) { let safeParse = (str) => str === 'undefined' ? undefined : JSON.parse(str) let theme = safeParse(localStorage.store_instanceThemes)[safeParse(localStorage.store_currentInstance)] if (theme && theme !== 'default') { @@ -18,14 +28,14 @@ if (localStorage.store_currentInstance && localStorage.store_instanceThemes) { } } -if (!localStorage.store_currentInstance) { +if (!hasLocalStorage || !localStorage.store_currentInstance) { // if not logged in, show all these 'hidden-from-ssr' elements let style = document.createElement('style') style.textContent = '.hidden-from-ssr { opacity: 1 !important; }' document.head.appendChild(style) } -if (localStorage.store_disableCustomScrollbars === 'true') { +if (hasLocalStorage && localStorage.store_disableCustomScrollbars === 'true') { // if user has disabled custom scrollbars, remove this style let theScrollbarStyle = document.getElementById('theScrollbarStyle') theScrollbarStyle.setAttribute('media', 'only x') // disables the style diff --git a/package-lock.json b/package-lock.json index a550c437..d138ef93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -6003,6 +6003,11 @@ "json5": "^0.5.0" } }, + "localstorage-memory": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/localstorage-memory/-/localstorage-memory-1.0.3.tgz", + "integrity": "sha512-t9P8WB6DcVttbw/W4PIE8HOqum8Qlvx5SjR6oInwR9Uia0EEmyUeBh7S+weKByW+l/f45Bj4L/dgZikGFDM6ng==" + }, "locate-path": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/locate-path/-/locate-path-2.0.0.tgz", diff --git a/package.json b/package.json index 008181aa..980e1cda 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "idb-keyval": "^3.1.0", "indexeddb-getall-shim": "^1.3.5", "intersection-observer": "^0.5.1", + "localstorage-memory": "^1.0.3", "lodash-es": "^4.17.11", "lodash-webpack-plugin": "^0.11.5", "mini-css-extract-plugin": "^0.4.5", diff --git a/routes/_pages/settings/instances/add.html b/routes/_pages/settings/instances/add.html index a36e7c3d..15a063ea 100644 --- a/routes/_pages/settings/instances/add.html +++ b/routes/_pages/settings/instances/add.html @@ -3,10 +3,18 @@
+ {#if !hasIndexedDB} + + {/if} + {#if $logInToInstanceError && $logInToInstanceErrorForText === $instanceNameInSearch} - + {/if}