Skip to content

Commit df44eea

Browse files
committed
Fixbugs in service worker
1 parent 330f5fc commit df44eea

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

src/common/logger.ts

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,24 @@
88
const STORAGE_KEY = "_logOpen"
99
const STORAGE_VAL = "1"
1010

11-
let OPEN_LOG = localStorage.getItem(STORAGE_KEY) === STORAGE_VAL
11+
let OPEN_LOG = false
12+
13+
// localStorage is not undefined in mv3 of service worker
14+
function initOpenLog() {
15+
try {
16+
localStorage.getItem(STORAGE_KEY) === STORAGE_VAL
17+
} catch (ignored) { }
18+
}
19+
20+
function updateLocalStorage(openState: boolean) {
21+
try {
22+
openState
23+
? localStorage.setItem(STORAGE_KEY, STORAGE_VAL)
24+
: localStorage.removeItem(STORAGE_KEY)
25+
} catch (ignored) { }
26+
}
27+
28+
initOpenLog()
1229

1330
/**
1431
* @since 0.0.4
@@ -22,16 +39,14 @@ export function log(...args: any) {
2239
* @since 0.0.4
2340
*/
2441
export function openLog(): string {
25-
OPEN_LOG = true
26-
localStorage.setItem(STORAGE_KEY, STORAGE_VAL)
42+
updateLocalStorage(OPEN_LOG = true)
2743
return 'Opened the log manually.'
2844
}
2945

3046
/**
3147
* @since 0.0.8
3248
*/
3349
export function closeLog(): string {
34-
OPEN_LOG = false
35-
localStorage.removeItem(STORAGE_KEY)
50+
updateLocalStorage(OPEN_LOG = false)
3651
return 'Closed the log manually.'
3752
}

0 commit comments

Comments
 (0)