-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
130 lines (106 loc) · 3.39 KB
/
Copy pathoptions.js
File metadata and controls
130 lines (106 loc) · 3.39 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
const domainInput = document.querySelector("#domain-input");
const addDomainButton = document.querySelector("#add-domain");
const domainList = document.querySelector("#domain-list");
const statusMessage = document.querySelector("#status-message");
function normalizeDomain(input) {
if (!input) {
return null;
}
let value = String(input).trim().toLowerCase();
if (!value) {
return null;
}
if (!value.includes("://")) {
value = `https://${value}`;
}
try {
const url = new URL(value);
let hostname = url.hostname.replace(/\.$/, "");
if (hostname.startsWith("www.")) {
hostname = hostname.slice(4);
}
return hostname || null;
} catch {
return null;
}
}
async function getBlockedDomains() {
const { blockedDomains = [] } = await chrome.storage.sync.get("blockedDomains");
return blockedDomains;
}
async function saveBlockedDomains(blockedDomains) {
await chrome.storage.sync.set({ blockedDomains });
}
function setStatus(message, isError = false) {
statusMessage.textContent = message;
statusMessage.dataset.state = isError ? "error" : "success";
}
function renderBlockedDomains(blockedDomains) {
domainList.textContent = "";
if (!blockedDomains.length) {
const emptyState = document.createElement("li");
emptyState.className = "empty-state";
emptyState.textContent = "No blocked websites yet.";
domainList.append(emptyState);
return;
}
blockedDomains.forEach((domain) => {
const item = document.createElement("li");
item.className = "domain-item";
const label = document.createElement("span");
label.className = "domain-pill";
label.textContent = domain;
const removeButton = document.createElement("button");
removeButton.className = "remove-button";
removeButton.type = "button";
removeButton.textContent = "Remove";
removeButton.addEventListener("click", () => {
void removeDomain(domain);
});
item.append(label, removeButton);
domainList.append(item);
});
}
async function refreshList() {
const blockedDomains = await getBlockedDomains();
renderBlockedDomains(blockedDomains);
}
async function addDomain(rawValue) {
const normalized = normalizeDomain(rawValue);
if (!normalized) {
setStatus("Enter a valid domain like tiktok.com.", true);
return;
}
const blockedDomains = await getBlockedDomains();
if (blockedDomains.includes(normalized)) {
setStatus("That domain is already on the block list.", true);
return;
}
const nextDomains = [...blockedDomains, normalized].sort();
await saveBlockedDomains(nextDomains);
domainInput.value = "";
await refreshList();
setStatus(`${normalized} added to your block list.`);
}
async function removeDomain(domainToRemove) {
const blockedDomains = await getBlockedDomains();
const nextDomains = blockedDomains.filter((domain) => domain !== domainToRemove);
await saveBlockedDomains(nextDomains);
await refreshList();
setStatus(`${domainToRemove} removed from the block list.`);
}
addDomainButton.addEventListener("click", () => {
void addDomain(domainInput.value);
});
domainInput.addEventListener("keydown", (event) => {
if (event.key === "Enter") {
event.preventDefault();
void addDomain(domainInput.value);
}
});
chrome.storage.onChanged.addListener((changes, areaName) => {
if (areaName === "sync" && changes.blockedDomains) {
void refreshList();
}
});
void refreshList();