-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
46 lines (41 loc) · 1.51 KB
/
popup.js
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
document.addEventListener("DOMContentLoaded", () => {
const blockForm = document.getElementById("block-form");
const siteInput = document.getElementById("site-input");
const blockList = document.getElementById("block-list");
chrome.storage.sync.get("blockedSites", (data) => {
const blockedSites = data.blockedSites || [];
blockedSites.forEach((site) => addSiteToList(site));
});
blockForm.addEventListener("submit", (e) => {
e.preventDefault();
const site = siteInput.value.trim();
if (site) {
chrome.storage.sync.get("blockedSites", (data) => {
const blockedSites = data.blockedSites || [];
if (!blockedSites.includes(site)) {
blockedSites.push(site);
chrome.storage.sync.set({ blockedSites }, () => {
addSiteToList(site);
siteInput.value = "";
});
}
});
}
});
function addSiteToList(site) {
const li = document.createElement("li");
li.textContent = site;
const removeBtn = document.createElement("button");
removeBtn.textContent = "Remove";
removeBtn.addEventListener("click", () => {
chrome.storage.sync.get("blockedSites", (data) => {
const blockedSites = data.blockedSites.filter((s) => s !== site);
chrome.storage.sync.set({ blockedSites }, () => {
li.remove();
});
});
});
li.appendChild(removeBtn);
blockList.appendChild(li);
}
});