Skip to content

Commit bcc86a6

Browse files
JOHNJOHN
authored andcommitted
Fix logger chrome.storage errors in offscreen document
- Add proper null checks for chrome and chrome.storage - Use optional chaining to safely check chrome.storage.local - Silently fail if storage unavailable (logging shouldn't break app) - Fixes 'Cannot read properties of undefined (reading local)' errors
1 parent dea0e98 commit bcc86a6

File tree

1 file changed

+27
-6
lines changed

1 file changed

+27
-6
lines changed

src/utils/logger.ts

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,19 @@ class Logger {
5151
private isStorageAvailable = false;
5252

5353
private constructor() {
54-
// Check if chrome.storage is available (not in content scripts)
55-
this.isStorageAvailable = typeof chrome !== 'undefined' &&
56-
typeof chrome.storage !== 'undefined' &&
57-
typeof chrome.storage.local !== 'undefined';
54+
// Check if chrome.storage is available
55+
// Must check chrome first, then chrome.storage, then chrome.storage.local
56+
try {
57+
this.isStorageAvailable = typeof chrome !== 'undefined' &&
58+
chrome !== null &&
59+
typeof chrome.storage !== 'undefined' &&
60+
chrome.storage !== null &&
61+
typeof chrome.storage.local !== 'undefined' &&
62+
chrome.storage.local !== null;
63+
} catch (e) {
64+
this.isStorageAvailable = false;
65+
}
66+
5867
if (this.isStorageAvailable) {
5968
this.loadLogs();
6069
}
@@ -70,21 +79,33 @@ class Logger {
7079
private async loadLogs() {
7180
if (!this.isStorageAvailable) return;
7281
try {
82+
// Double-check availability before use
83+
if (typeof chrome === 'undefined' || !chrome?.storage?.local) {
84+
this.isStorageAvailable = false;
85+
return;
86+
}
7387
const result = await chrome.storage.local.get('debugLogs');
7488
if (result.debugLogs) {
7589
this.logBuffer = result.debugLogs;
7690
}
7791
} catch (error) {
78-
console.error('Failed to load logs:', error);
92+
// Silently fail - logging shouldn't break the app
93+
this.isStorageAvailable = false;
7994
}
8095
}
8196

8297
private async saveLogs() {
8398
if (!this.isStorageAvailable) return;
8499
try {
100+
// Double-check availability before use
101+
if (typeof chrome === 'undefined' || !chrome?.storage?.local) {
102+
this.isStorageAvailable = false;
103+
return;
104+
}
85105
await chrome.storage.local.set({ debugLogs: this.logBuffer });
86106
} catch (error) {
87-
console.error('Failed to save logs:', error);
107+
// Silently fail - logging shouldn't break the app
108+
this.isStorageAvailable = false;
88109
}
89110
}
90111

0 commit comments

Comments
 (0)