-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
popup.js
48 lines (41 loc) · 1.6 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
47
48
const isNodeEnv = typeof exports !== 'undefined'
// Chrome support: `browser` should fallback to `chrome`
// since Chrome doesn't fully support WebExtensions
if (typeof browser === 'undefined' && !isNodeEnv) {
browser = chrome
}
document.addEventListener('DOMContentLoaded', function() {
const statusElement = document.getElementById('status');
const toggleButton = document.getElementById('toggleButton');
function updateUI(isEnabled) {
statusElement.textContent = isEnabled ? 'Enabled' : 'Disabled';
statusElement.style.color = isEnabled ? '#4CAF50' : '#FF5252';
toggleButton.textContent = isEnabled ? 'Disable' : 'Enable';
toggleButton.style.backgroundColor = isEnabled ? '#FF5252' : '#4CAF50';
}
function toggleExtension() {
browser.storage.local.get('enabled', function(data) {
const newState = !data.enabled;
browser.storage.local.set({enabled: newState}, function() {
updateUI(newState);
updateIcon(newState);
});
});
}
function updateIcon(isEnabled) {
const iconPath = isEnabled ? "icon-enabled.png" : "icon-disabled.png";
browser.browserAction.setIcon({path: iconPath}, function() {
if (browser.runtime.lastError) {
console.error("Error updating icon:", browser.runtime.lastError);
} else {
console.log("Icon updated successfully");
}
});
}
toggleButton.addEventListener('click', toggleExtension);
browser.storage.local.get('enabled', function(data) {
const isEnabled = data.enabled !== false; // Default to true if not set
updateUI(isEnabled);
updateIcon(isEnabled);
});
});