-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
134 lines (112 loc) · 3.98 KB
/
options.js
File metadata and controls
134 lines (112 loc) · 3.98 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
// options.js
document
.getElementById("urlInput")
.addEventListener("keypress", function (event) {
if (event.key === "Enter") {
document.getElementById("addButton").click();
}
});
document.addEventListener("DOMContentLoaded", function () {
const urlInput = document.getElementById("urlInput");
const addButton = document.getElementById("addButton");
const blockedList = document.getElementById("blockedList");
// Load saved blocked sites
function loadBlockedSites() {
chrome.storage.local.get(["blockedSites"], function (result) {
blockedList.innerHTML = ""; // Clear list before rendering
if (result.blockedSites) {
result.blockedSites.forEach((site) => addUrlToList(site));
}
});
}
// Validate input URL
function isValidUrl(url) {
try {
new URL(url.startsWith("http") ? url : `https://${url}`);
return true;
} catch (_) {
return false;
}
}
// Add URL to list and storage with dynamic truncation
function addUrlToList(url) {
const listItem = document.createElement("li");
listItem.classList.add("url-item");
const urlSpan = document.createElement("span");
urlSpan.className = "url-text";
urlSpan.textContent = url;
urlSpan.title = url; // Show full URL on hover
const removeBtn = document.createElement("button");
removeBtn.className = "removeButton";
removeBtn.textContent = "Remove";
removeBtn.dataset.url = url;
listItem.appendChild(urlSpan);
listItem.appendChild(removeBtn);
blockedList.appendChild(listItem);
// Adjust truncation on window resize
window.addEventListener("resize", function () {
adjustUrlDisplay(urlSpan);
});
// Initial adjustment
adjustUrlDisplay(urlSpan);
}
// Dynamically adjust URL display based on available space
function adjustUrlDisplay(element) {
// Temporarily show full text to measure width
element.style.textOverflow = "clip";
element.style.overflow = "visible";
element.style.whiteSpace = "nowrap";
const fullWidth = element.scrollWidth;
const availableWidth = element.parentElement.offsetWidth - 100; // Space for button
// Restore ellipsis if needed
element.style.textOverflow = "ellipsis";
element.style.overflow = "hidden";
// Show tooltip only if text is truncated
element.title = fullWidth > availableWidth ? element.textContent : "";
}
// Handle adding a new blocked site
addButton.addEventListener("click", function () {
let url = urlInput.value.trim();
// Add https:// if not present
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = `https://${url}`;
}
if (!isValidUrl(url)) {
return; // Simply return if the URL is invalid
}
chrome.storage.local.get(["blockedSites"], function (result) {
let blockedSites = result.blockedSites || [];
if (blockedSites.includes(url)) {
return; // Return if the URL is already blocked
}
blockedSites.push(url);
chrome.storage.local.set({ blockedSites }, function () {
if (chrome.runtime.lastError) {
return; // Handle error silently
}
addUrlToList(url);
urlInput.value = ""; // Clear input after adding
urlInput.focus(); // Focus back on input
});
});
});
// Handle removing a blocked site
blockedList.addEventListener("click", function (event) {
if (event.target.classList.contains("removeButton")) {
const urlToRemove = event.target.getAttribute("data-url");
chrome.storage.local.get(["blockedSites"], function (result) {
let blockedSites = result.blockedSites.filter(
(site) => site !== urlToRemove
);
chrome.storage.local.set({ blockedSites }, function () {
if (chrome.runtime.lastError) {
return; // Handle error silently
}
loadBlockedSites(); // Refresh list after deletion
});
});
}
});
// Load stored sites on page load
loadBlockedSites();
});