-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
43 lines (36 loc) · 1.11 KB
/
popup.js
File metadata and controls
43 lines (36 loc) · 1.11 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
let noteField;
let saveStatus;
const unsavedStateText = "Unsaved changes...";
const savedStateText = "All changes saved";
document.addEventListener("DOMContentLoaded", async () => {
noteField = document.getElementById("note");
saveStatus = document.getElementById("saveStatus");
const note = await chrome.runtime.sendMessage({ action: "getNote" });
if (note) {
noteField.innerHTML = note
}
saveStatus.textContent = savedStateText
noteField.addEventListener("input", () => {
saveStatus.textContent = unsavedStateText;
debounceSave();
});
});
function debounce(timeout, func) {
return () => {
clearTimeout(timeout);
timeout = setTimeout(func, 1000);
}
}
const debounceSave = debounce(1000, save);
async function save() {
const key = await chrome.runtime.sendMessage({ action: "getSavingKey" });
if (noteField.innerHTML === "<br>") {
noteField.innerHTML = "";
}
const note = noteField.innerHTML;
let saveData = {};
saveData[key] = note;
chrome.storage.local.set(saveData);
chrome.runtime.sendMessage({ action: "refreshIcon" });
saveStatus.textContent = savedStateText;
}