-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
68 lines (59 loc) · 1.89 KB
/
popup.js
File metadata and controls
68 lines (59 loc) · 1.89 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
const listEl = document.getElementById("keywordList");
const inputEl = document.getElementById("newKeyword");
const addBtn = document.getElementById("addBtn");
const sortNowBtn = document.getElementById("sortNowBtn");
// --- Render ---
async function render() {
const { keywords = [] } = await chrome.storage.sync.get("keywords");
listEl.innerHTML = "";
for (const kw of keywords) {
const li = document.createElement("li");
const span = document.createElement("span");
span.textContent = kw;
const btn = document.createElement("button");
btn.className = "remove-btn";
btn.textContent = "\u00d7";
btn.addEventListener("click", () => removeKeyword(kw));
li.appendChild(span);
li.appendChild(btn);
listEl.appendChild(li);
}
}
// --- Add ---
async function addKeyword() {
const value = inputEl.value.trim();
if (!value) return;
const { keywords = [] } = await chrome.storage.sync.get("keywords");
if (keywords.some((k) => k.toLowerCase() === value.toLowerCase())) {
inputEl.value = "";
return; // duplicate
}
keywords.push(value);
await chrome.storage.sync.set({ keywords });
inputEl.value = "";
render();
}
// --- Remove ---
async function removeKeyword(kw) {
const { keywords = [] } = await chrome.storage.sync.get("keywords");
const updated = keywords.filter((k) => k !== kw);
await chrome.storage.sync.set({ keywords: updated });
render();
}
// --- Sort Now ---
sortNowBtn.addEventListener("click", () => {
sortNowBtn.textContent = "Sorting\u2026";
sortNowBtn.disabled = true;
chrome.runtime.sendMessage({ action: "sortNow" }, () => {
sortNowBtn.textContent = "Done!";
setTimeout(() => {
sortNowBtn.textContent = "Sort Now";
sortNowBtn.disabled = false;
}, 1000);
});
});
addBtn.addEventListener("click", addKeyword);
inputEl.addEventListener("keydown", (e) => {
if (e.key === "Enter") addKeyword();
});
render();