Skip to content

Commit

Permalink
fix: don't use localStorage when browser blocks access (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcosvega91 committed Oct 30, 2020
1 parent 0e6256b commit d5e3d81
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 6 deletions.
9 changes: 7 additions & 2 deletions devtools/src/devtools/lib/settings.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import Bridge from 'crx-bridge';

const localSettings = JSON.parse(localStorage.getItem('playground_settings'));
const localSettings = navigator.cookieEnabled
? JSON.parse(localStorage.getItem('playground_settings'))
: {};

let _settings = Object.assign(
{
testIdAttribute: 'data-testid',
Expand All @@ -17,5 +20,7 @@ export function getSettings() {
export function setSettings(settings) {
Object.assign(_settings, settings);
Bridge.sendMessage('SET_SETTINGS', _settings, 'content-script');
localStorage.setItem('playground_settings', JSON.stringify(_settings));
if (navigator.cookieEnabled) {
localStorage.setItem('playground_settings', JSON.stringify(_settings));
}
}
11 changes: 10 additions & 1 deletion src/components/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,18 @@ function Settings({ settings, dispatch }) {

const showBehavior = typeof settings.autoRun !== 'undefined';
const showTestingLibrary = typeof settings.testIdAttribute !== 'undefined';
const isCookieEanbled = navigator.cookieEnabled;

return (
<div className="settings text-sm pb-2">
<div className="settings text-sm pb-2 ">
{!isCookieEanbled && (
<p
className="text-sm font-bold text-orange-600 border border-orange-600 px-3 py-1"
role="alert"
>
Cookie are not enabled, settings will not be saved.
</p>
)}
<form onChange={handleChange} onSubmit={(e) => e.preventDefault()}>
{showBehavior && (
<div>
Expand Down
11 changes: 9 additions & 2 deletions src/hooks/usePlayground.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,12 @@ const effectMap = {
},

SAVE_SETTINGS: (state) => {
localStorage.setItem('playground_settings', JSON.stringify(state.settings));
if (navigator.cookieEnabled) {
localStorage.setItem(
'playground_settings',
JSON.stringify(state.settings),
);
}
},

APPLY_SETTINGS: (state, effect, dispatch) => {
Expand Down Expand Up @@ -259,7 +264,9 @@ const effectMap = {
};

function getInitialState(props) {
const localSettings = JSON.parse(localStorage.getItem('playground_settings'));
const localSettings = navigator.cookieEnabled
? JSON.parse(localStorage.getItem('playground_settings'))
: {};

const state = {
...props,
Expand Down
3 changes: 2 additions & 1 deletion src/lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ function renderDiff(diff) {
// when debug is `undefined` and can be enabled by setting it to either `true`
// or `diff`. On develop, it's enabled when `undefined`, and can be disabled
// by setting it to `false`.
const debug = localStorage.getItem('debug');
const debug = navigator.cookieEnabled ? localStorage.getItem('debug') : 'false';

const logLevel = debug === 'false' ? false : debug === 'true' ? true : debug;
const isLoggingEnabled =
process.env.NODE_ENV === 'development' ? logLevel !== false : !!logLevel;
Expand Down

0 comments on commit d5e3d81

Please sign in to comment.