-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpopup.js
More file actions
75 lines (65 loc) · 2.13 KB
/
popup.js
File metadata and controls
75 lines (65 loc) · 2.13 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
document.addEventListener("DOMContentLoaded", () => {
const clearPreferencesButton = document.getElementById("clearPreferences");
const statusElement = document.getElementById("status");
/**
* Gets the active tab's domain
* @returns {Promise<string>} Promise that resolves to the active domain
*/
async function getActiveDomain() {
return new Promise((resolve) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
if (tabs.length === 0) {
resolve(null);
return;
}
try {
const url = new URL(tabs[0].url);
resolve(url.hostname);
} catch (error) {
console.error("Error parsing URL:", error);
resolve(null);
}
});
});
}
/**
* Shows a status message that fades out
* @param {string} message - The message to display
* @param {string} type - The type of message (success or error)
*/
function showStatus(message, type = "success") {
statusElement.textContent = message;
statusElement.className = `status ${type}`;
statusElement.style.opacity = "1";
setTimeout(() => {
statusElement.style.opacity = "0";
}, 3000);
}
/**
* Clears site preferences for the active domain
*/
async function clearSitePreferences() {
const site = await getActiveDomain();
if (!site) {
showStatus("Unable to determine current site", "error");
return;
}
chrome.storage.sync.get({ sitePreferences: {} }, (data) => {
const sitePreferences = data.sitePreferences;
if (sitePreferences.hasOwnProperty(site)) {
delete sitePreferences[site];
chrome.storage.sync.set({ sitePreferences }, () => {
if (chrome.runtime.lastError) {
showStatus(`Error: ${chrome.runtime.lastError.message}`, "error");
} else {
showStatus(`Preferences cleared for ${site}`);
}
});
} else {
showStatus(`No preferences found for ${site}`);
}
});
}
// Add click event for clear preferences button
clearPreferencesButton.addEventListener("click", clearSitePreferences);
});