From d3bc08488eced44dae58c7de37095d170c97d01d Mon Sep 17 00:00:00 2001 From: dnson Date: Wed, 3 Mar 2021 09:18:55 +0700 Subject: [PATCH] Check localStorage permission (#212) --- lib/plugins/consent-manager.js | 6 ++++-- lib/record.js | 5 +++++ lib/utils/misc.js | 14 +++++++++++++- package.json | 3 ++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/lib/plugins/consent-manager.js b/lib/plugins/consent-manager.js index 6094c8ee..8e553866 100644 --- a/lib/plugins/consent-manager.js +++ b/lib/plugins/consent-manager.js @@ -2,7 +2,7 @@ import _ from '../utils/lodash' import dayjs from 'dayjs' import global from 'global' import generateUUID from '../utils/generateUUID' -import { camelCase } from '../utils/misc' +import { camelCase, isLocalStorageAccessible } from '../utils/misc' require('es6-promise').polyfill() @@ -178,6 +178,8 @@ export default { }, getPreferences () { + if (!isLocalStorageAccessible()) return null + const persistedPreferences = JSON.parse(global.localStorage.getItem(this.consentManager.storageKey)) || null if (persistedPreferences) { @@ -199,7 +201,7 @@ export default { }, _savePreferences () { - if (_.isEmpty(this.consentManager.preferences)) return + if (!isLocalStorageAccessible() || _.isEmpty(this.consentManager.preferences)) return global.localStorage.setItem( this.consentManager.storageKey, diff --git a/lib/record.js b/lib/record.js index 911a37f1..a624f8bc 100644 --- a/lib/record.js +++ b/lib/record.js @@ -72,6 +72,7 @@ exports.areEventsBlocked = function areEventsBlocked () { */ exports.setSignedMode = function setSignedMode () { if (this.client.storeConsentByLocalStorage) { + if (!misc.isLocalStorageAccessible()) return this global.localStorage.setItem(SIGNEDMODECOOKIE, 'true') } else { setCookie(this.client.storage, SIGNEDMODECOOKIE, 'true') @@ -88,6 +89,8 @@ exports.setSignedMode = function setSignedMode () { */ exports.setAnonymousMode = function setAnonymousMode (keepIdentifier) { if (this.client.storeConsentByLocalStorage) { + if (!misc.isLocalStorageAccessible()) return this + global.localStorage.setItem(SIGNEDMODECOOKIE, 'false') } else { setCookie(this.client.storage, SIGNEDMODECOOKIE, 'false') @@ -114,6 +117,8 @@ exports.setAnonymousMode = function setAnonymousMode (keepIdentifier) { */ exports.inSignedMode = function inSignedMode () { if (this.client.storeConsentByLocalStorage) { + if (!misc.isLocalStorageAccessible()) return false + return global.localStorage.getItem([SIGNEDMODECOOKIE]) !== 'false' && (global.localStorage.getItem([SIGNEDMODECOOKIE]) === 'true' || this.client.startInSignedMode) diff --git a/lib/utils/misc.js b/lib/utils/misc.js index 0086bf1a..4b42b6a7 100644 --- a/lib/utils/misc.js +++ b/lib/utils/misc.js @@ -48,6 +48,17 @@ function capitalizeFirstLetter (str) { return str.slice(0, 2).toUpperCase() + str.slice(2) } +function isLocalStorageAccessible () { + var test = '__td__' + try { + localStorage.setItem(test, test) + localStorage.removeItem(test) + return true + } catch (e) { + return false + } +} + function camelCase (str) { if (!str) return @@ -66,5 +77,6 @@ module.exports = { disposable: disposable, invariant: invariant, fetchWithTimeout: fetchWithTimeout, - camelCase: camelCase + camelCase: camelCase, + isLocalStorageAccessible: isLocalStorageAccessible } diff --git a/package.json b/package.json index b6817272..3b387f10 100644 --- a/package.json +++ b/package.json @@ -40,7 +40,8 @@ "afterEach", "beforeEach", "describe", - "it" + "it", + "localStorage" ] }, "prettier": {