forked from nsht/tab_notes
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
258 lines (231 loc) · 7.5 KB
/
script.js
File metadata and controls
258 lines (231 loc) · 7.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
const els = {
html: document.documentElement,
note: document.getElementById("notes"),
toggleTheme: document.getElementById("theme-toggle"),
toggleSettings: document.getElementById("settings-toggle"),
menu: document.getElementById("settings-menu"),
slider: document.getElementById("font-slider"),
fontDisplay: document.getElementById("font-display"),
fontFamily: document.getElementById("font-family"),
words: document.getElementById("word-count"),
status: document.getElementById("save-status"),
};
// Detect browser API (browser vs chrome)
const browserAPI = typeof browser !== "undefined" ? browser : chrome;
// Storage Wrapper - now uses browser.storage.sync with proper error handling
const store = {
get: (key, callback) => {
const handleError = (error) => {
if (error) {
console.error("Storage get error:", error);
els.status.textContent = "Error loading";
}
};
if (typeof callback === "function") {
// Chrome-style callback with error handling
browserAPI.storage.sync.get([key], (result) => {
if (browserAPI.runtime.lastError) {
handleError(browserAPI.runtime.lastError);
return;
}
callback(result);
});
} else {
// Promise-style (Firefox) with error handling
return browserAPI.storage.sync.get([key]).catch(handleError);
}
},
set: (key, value, callback) => {
const data = {};
data[key] = value;
const handleError = (error) => {
if (error) {
console.error("Storage set error:", error);
els.status.textContent = "Error saving";
els.status.classList.remove("saved");
}
};
if (typeof callback === "function") {
browserAPI.storage.sync.set(data, () => {
if (browserAPI.runtime.lastError) {
handleError(browserAPI.runtime.lastError);
return;
}
callback();
});
} else {
browserAPI.storage.sync.set(data).catch(handleError);
}
},
};
// LocalStorage fallback (for theme/font settings that don't need sync)
const localStore = {
get: (k) => {
try {
return localStorage.getItem(k);
} catch (e) {
console.error("LocalStorage get error:", e);
}
},
set: (k, v) => {
try {
localStorage.setItem(k, v);
} catch (e) {
console.error("LocalStorage set error:", e);
}
},
};
// Theme Logic
const toggleTheme = () => {
const next =
els.html.getAttribute("data-theme") === "dark" ? "light" : "dark";
els.html.setAttribute("data-theme", next);
localStore.set("theme", next);
els.toggleTheme.setAttribute(
"aria-label",
`Switch to ${next === "dark" ? "light" : "dark"}`
);
};
els.toggleTheme.addEventListener("click", toggleTheme);
// Settings & Menu Logic
const toggleMenu = (e) => {
e.stopPropagation();
const isHidden = els.menu.hidden;
els.menu.hidden = !isHidden;
els.toggleSettings.setAttribute("aria-expanded", !isHidden);
};
const updateFontSize = (val) => {
const size = `${val}px`;
els.html.style.setProperty("--note-size", size);
els.fontDisplay.textContent = size;
localStore.set("fontsize", size);
};
const updateFontFamily = (val) => {
els.html.style.setProperty("--font-body", val);
localStore.set("fontfamily", val);
};
// Menu Listeners
els.toggleSettings.addEventListener("click", toggleMenu);
els.slider.addEventListener("input", (e) => {
updateFontSize(e.target.value);
});
els.fontFamily.addEventListener("change", (e) => {
updateFontFamily(e.target.value);
});
// Close menu when clicking outside
document.addEventListener("click", (e) => {
if (
!els.menu.hidden &&
!els.menu.contains(e.target) &&
e.target !== els.toggleSettings
) {
els.menu.hidden = true;
els.toggleSettings.setAttribute("aria-expanded", "false");
}
});
// Stats Logic
const updateStats = (text) => {
const count = text.trim() ? text.trim().split(/\s+/).length : 0;
els.words.textContent = `${count} word${count === 1 ? "" : "s"}`;
};
// Save Logic (Debounced)
let timeout;
const save = (text) => {
els.status.textContent = "Saving...";
els.status.classList.remove("saved");
clearTimeout(timeout);
timeout = setTimeout(() => {
store.set("notekeeper_content", text, () => {
// Only update UI on success (error handling is in store.set)
els.status.textContent = "Saved";
els.status.classList.add("saved");
});
}, 500);
};
// Listen for storage changes from other tabs/extensions
// This keeps notes in sync across multiple tabs
browserAPI.storage.onChanged.addListener((changes, area) => {
if (area === "sync" && changes.notekeeper_content) {
const newValue = changes.notekeeper_content.newValue;
if (newValue !== undefined && newValue !== els.note.value) {
els.note.value = newValue;
updateStats(newValue);
els.status.textContent = "Synced";
els.status.classList.add("saved");
}
}
});
// Refresh when the page gains visibility (e.g., switching back to tab)
// This handles tab switching without requiring tabs permission
document.addEventListener("visibilitychange", () => {
if (!document.hidden) {
store.get("notekeeper_content", (result) => {
if (result && result.notekeeper_content) {
els.note.value = result.notekeeper_content;
updateStats(result.notekeeper_content);
els.status.textContent = "Restored";
els.status.classList.add("saved");
}
});
}
});
// Initialize - load from sync storage and run migration if needed
// Combined into single operation to avoid race condition
const initializeNotes = () => {
store.get("notekeeper_content", (syncResult) => {
if (syncResult && syncResult.notekeeper_content) {
// We have data in sync storage, load it
els.note.value = syncResult.notekeeper_content;
updateStats(syncResult.notekeeper_content);
els.status.textContent = "Restored";
els.status.classList.add("saved");
return;
}
// No sync data, check for localStorage migration
const localContent = localStore.get("notekeeper_content");
if (localContent) {
console.log("Migrating notes from localStorage to sync storage...");
store.set("notekeeper_content", localContent, () => {
els.note.value = localContent;
updateStats(localContent);
els.status.textContent = "Migrated";
els.status.classList.add("saved");
});
return;
}
// Check for legacy tab_note (from original extension)
browserAPI.storage.sync.get(["tab_note"], (legacyResult) => {
if (browserAPI.runtime.lastError) {
console.error("Legacy storage check error:", browserAPI.runtime.lastError);
return;
}
if (legacyResult && legacyResult.tab_note) {
console.log("Migrating notes from legacy tab_note to notekeeper_content...");
store.set("notekeeper_content", legacyResult.tab_note, () => {
els.note.value = legacyResult.tab_note;
updateStats(legacyResult.tab_note);
els.status.textContent = "Restored";
els.status.classList.add("saved");
});
}
});
});
};
initializeNotes();
// Init Font Size
const storedFontSize = localStore.get("fontsize");
if (storedFontSize) {
const val = parseInt(storedFontSize);
els.slider.value = val;
updateFontSize(val);
}
// Init Font Family
const storedFontFamily = localStore.get("fontfamily");
if (storedFontFamily) {
els.fontFamily.value = storedFontFamily;
updateFontFamily(storedFontFamily);
}
els.note.addEventListener("input", (e) => {
updateStats(e.target.value);
save(e.target.value);
});