-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
36 lines (31 loc) · 1.13 KB
/
background.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
// background.js
// Function to update the badge text based on the active state
function updateBadge() {
chrome.storage.local.get('isActive', (data) => {
const badgeText = data.isActive ? 'ON' : 'OFF';
const badgeColor = data.isActive ? '#008000' : '#DC3545'; // Green for ON, Red for OFF
chrome.action.setBadgeText({ text: badgeText });
chrome.action.setBadgeBackgroundColor({ color: badgeColor }); // Set badge color
});
}
// Set default state to true (extension is active) when installed
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ isActive: true }, () => {
updateBadge();
});
});
// Update badge when the extension starts
chrome.runtime.onStartup.addListener(() => {
updateBadge();
});
// When the user clicks the extension icon
chrome.action.onClicked.addListener((tab) => {
chrome.storage.local.get('isActive', (data) => {
const newState = !data.isActive;
chrome.storage.local.set({ isActive: newState }, () => {
updateBadge();
// Send a message to the content script to update its state
chrome.tabs.sendMessage(tab.id, { isActive: newState });
});
});
});