Repository navigation
Expand file tree
/
Copy pathpopup.js
More file actions
237 lines (193 loc) · 5.84 KB
/
Copy pathpopup.js
File metadata and controls
237 lines (193 loc) · 5.84 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
/**
* STFU Popup Script
* Handles UI interactions for managing URL prefixes
*/
// DOM elements
const newPrefixInput = document.getElementById("newPrefix");
const addButton = document.getElementById("addButton");
const addCurrentButton = document.getElementById("addCurrentButton");
const prefixList = document.getElementById("prefixList");
/**
* Loads and displays all prefixes from storage
*/
async function loadPrefixes() {
const data = await browser.storage.local.get("prefixes");
const prefixes = data.prefixes || [];
if (prefixes.length === 0) {
prefixList.innerHTML =
'<p class="empty-message">No URL prefixes added yet.</p>';
return;
}
// Sort prefixes alphabetically
prefixes.sort((a, b) => a.url.localeCompare(b.url));
prefixList.innerHTML = "";
prefixes.forEach((prefix, index) => {
const item = createPrefixItem(prefix, index);
prefixList.appendChild(item);
});
}
/**
* Creates a DOM element for a prefix item
* @param {Object} prefix - The prefix object {url, enabled}
* @param {number} index - The index in the array
* @returns {HTMLElement} The prefix item element
*/
function createPrefixItem(prefix, index) {
const item = document.createElement("div");
item.className = "prefix-item";
// Toggle checkbox
const toggle = document.createElement("input");
toggle.type = "checkbox";
toggle.className = "prefix-toggle";
toggle.checked = prefix.enabled;
toggle.addEventListener("change", () => togglePrefix(index));
// URL label
const label = document.createElement("span");
label.className = "prefix-url";
label.textContent = prefix.url;
label.title = prefix.url;
// Delete button
const deleteBtn = document.createElement("button");
deleteBtn.className = "btn-delete";
deleteBtn.textContent = "×";
deleteBtn.title = "Remove prefix";
deleteBtn.addEventListener("click", () => deletePrefix(index));
item.appendChild(toggle);
item.appendChild(label);
item.appendChild(deleteBtn);
return item;
}
/**
* Adds a new prefix to storage
* @param {string} url - The URL prefix to add
*/
async function addPrefix(url) {
// Validate and normalize URL
url = url.trim();
if (!url) {
showMessage("Please enter a URL prefix", "error");
return;
}
// Ensure URL has a protocol
if (!url.startsWith("http://") && !url.startsWith("https://")) {
url = "https://" + url;
}
// Validate URL format
try {
new URL(url);
} catch (error) {
showMessage("Invalid URL format", "error");
return;
}
const data = await browser.storage.local.get("prefixes");
const prefixes = data.prefixes || [];
// Check for duplicates
if (prefixes.some((p) => p.url === url)) {
showMessage("This prefix already exists", "error");
return;
}
// Add new prefix (enabled by default)
prefixes.push({ url, enabled: true });
await browser.storage.local.set({ prefixes });
// Clear input and reload list
newPrefixInput.value = "";
await loadPrefixes();
showMessage("Prefix added successfully", "success");
// Notify background script to refresh tabs
browser.runtime.sendMessage({ action: "refreshTabs" });
}
/**
* Toggles the enabled state of a prefix
* @param {number} index - The index of the prefix to toggle
*/
async function togglePrefix(index) {
const data = await browser.storage.local.get("prefixes");
const prefixes = data.prefixes || [];
if (prefixes[index]) {
prefixes[index].enabled = !prefixes[index].enabled;
await browser.storage.local.set({ prefixes });
// Notify background script to refresh tabs
browser.runtime.sendMessage({ action: "refreshTabs" });
}
}
/**
* Deletes a prefix from storage
* @param {number} index - The index of the prefix to delete
*/
async function deletePrefix(index) {
const data = await browser.storage.local.get("prefixes");
const prefixes = data.prefixes || [];
if (prefixes[index]) {
prefixes.splice(index, 1);
await browser.storage.local.set({ prefixes });
await loadPrefixes();
showMessage("Prefix removed", "success");
// Notify background script to refresh tabs
browser.runtime.sendMessage({ action: "refreshTabs" });
}
}
/**
* Adds the current tab's origin as a prefix
*/
async function addCurrentTab() {
try {
const tabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
if (tabs.length === 0) {
showMessage("No active tab found", "error");
return;
}
const tab = tabs[0];
if (
!tab.url ||
tab.url.startsWith("about:") ||
tab.url.startsWith("moz-extension:")
) {
showMessage("Cannot add this type of page", "error");
return;
}
// Extract origin (protocol + hostname)
const url = new URL(tab.url);
const origin = url.origin;
await addPrefix(origin);
} catch (error) {
console.error("Error adding current tab:", error);
showMessage("Error adding current tab", "error");
}
}
/**
* Shows a temporary message to the user
* @param {string} text - The message text
* @param {string} type - The message type ('success' or 'error')
*/
function showMessage(text, type) {
// Remove any existing message
const existing = document.querySelector(".message");
if (existing) {
existing.remove();
}
const message = document.createElement("div");
message.className = `message message-${type}`;
message.textContent = text;
document
.querySelector(".container")
.insertBefore(message, document.querySelector(".add-section"));
// Remove after 3 seconds
setTimeout(() => {
message.remove();
}, 3000);
}
// Event listeners
addButton.addEventListener("click", () => {
addPrefix(newPrefixInput.value);
});
newPrefixInput.addEventListener("keypress", (e) => {
if (e.key === "Enter") {
addPrefix(newPrefixInput.value);
}
});
addCurrentButton.addEventListener("click", addCurrentTab);
// Load prefixes when popup opens
loadPrefixes();