Skip to content

Commit d5e3d81

Browse files
authored
fix: don't use localStorage when browser blocks access (#287)
1 parent 0e6256b commit d5e3d81

File tree

4 files changed

+28
-6
lines changed

4 files changed

+28
-6
lines changed

devtools/src/devtools/lib/settings.js

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
import Bridge from 'crx-bridge';
22

3-
const localSettings = JSON.parse(localStorage.getItem('playground_settings'));
3+
const localSettings = navigator.cookieEnabled
4+
? JSON.parse(localStorage.getItem('playground_settings'))
5+
: {};
6+
47
let _settings = Object.assign(
58
{
69
testIdAttribute: 'data-testid',
@@ -17,5 +20,7 @@ export function getSettings() {
1720
export function setSettings(settings) {
1821
Object.assign(_settings, settings);
1922
Bridge.sendMessage('SET_SETTINGS', _settings, 'content-script');
20-
localStorage.setItem('playground_settings', JSON.stringify(_settings));
23+
if (navigator.cookieEnabled) {
24+
localStorage.setItem('playground_settings', JSON.stringify(_settings));
25+
}
2126
}

src/components/Settings.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,18 @@ function Settings({ settings, dispatch }) {
2222

2323
const showBehavior = typeof settings.autoRun !== 'undefined';
2424
const showTestingLibrary = typeof settings.testIdAttribute !== 'undefined';
25+
const isCookieEanbled = navigator.cookieEnabled;
2526

2627
return (
27-
<div className="settings text-sm pb-2">
28+
<div className="settings text-sm pb-2 ">
29+
{!isCookieEanbled && (
30+
<p
31+
className="text-sm font-bold text-orange-600 border border-orange-600 px-3 py-1"
32+
role="alert"
33+
>
34+
Cookie are not enabled, settings will not be saved.
35+
</p>
36+
)}
2837
<form onChange={handleChange} onSubmit={(e) => e.preventDefault()}>
2938
{showBehavior && (
3039
<div>

src/hooks/usePlayground.js

+9-2
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,12 @@ const effectMap = {
207207
},
208208

209209
SAVE_SETTINGS: (state) => {
210-
localStorage.setItem('playground_settings', JSON.stringify(state.settings));
210+
if (navigator.cookieEnabled) {
211+
localStorage.setItem(
212+
'playground_settings',
213+
JSON.stringify(state.settings),
214+
);
215+
}
211216
},
212217

213218
APPLY_SETTINGS: (state, effect, dispatch) => {
@@ -259,7 +264,9 @@ const effectMap = {
259264
};
260265

261266
function getInitialState(props) {
262-
const localSettings = JSON.parse(localStorage.getItem('playground_settings'));
267+
const localSettings = navigator.cookieEnabled
268+
? JSON.parse(localStorage.getItem('playground_settings'))
269+
: {};
263270

264271
const state = {
265272
...props,

src/lib/logger.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ function renderDiff(diff) {
7171
// when debug is `undefined` and can be enabled by setting it to either `true`
7272
// or `diff`. On develop, it's enabled when `undefined`, and can be disabled
7373
// by setting it to `false`.
74-
const debug = localStorage.getItem('debug');
74+
const debug = navigator.cookieEnabled ? localStorage.getItem('debug') : 'false';
75+
7576
const logLevel = debug === 'false' ? false : debug === 'true' ? true : debug;
7677
const isLoggingEnabled =
7778
process.env.NODE_ENV === 'development' ? logLevel !== false : !!logLevel;

0 commit comments

Comments
 (0)