-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
67 lines (58 loc) · 2.32 KB
/
background.js
File metadata and controls
67 lines (58 loc) · 2.32 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
// Background script to manage extension state
// Initialize default state on install
chrome.runtime.onInstalled.addListener(() => {
chrome.storage.local.set({ hypeLessEnabled: true });
});
// Handle messages from popup and content scripts
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
// --- Get current extension state ---
if (message.type === 'getState') {
chrome.storage.local.get(['hypeLessEnabled']).then(result => {
const enabled = result.hypeLessEnabled !== undefined ? result.hypeLessEnabled : true;
sendResponse({ enabled });
}).catch(error => {
console.error('Error getting state:', error);
sendResponse({ enabled: true }); // fallback
});
return true; // keep channel open for async
}
// --- Toggle extension on/off ---
else if (message.type === 'toggleExtension') {
chrome.storage.local.get(['hypeLessEnabled']).then(async result => {
const currentState = result.hypeLessEnabled !== undefined ? result.hypeLessEnabled : true;
const newState = !currentState;
try {
await chrome.storage.local.set({ hypeLessEnabled: newState });
// Notify all tabs of the state change
const tabs = await chrome.tabs.query({});
for (const tab of tabs) {
if (tab.id) {
chrome.tabs.sendMessage(tab.id, {
type: 'stateChanged',
enabled: newState
}).catch(() => {}); // silently ignore if content script not loaded
}
}
sendResponse({ enabled: newState });
} catch (error) {
console.error('Error toggling extension:', error);
sendResponse({ enabled: currentState });
}
}).catch(error => {
console.error('Error in toggle:', error);
sendResponse({ enabled: true });
});
return true; // async response
}
// --- Toggle sidebar ---
else if (message.type === 'toggleSidebar') {
chrome.tabs.query({ active: true, currentWindow: true }).then(tabs => {
const tab = tabs[0];
if (!tab || !tab.id || !tab.url.startsWith('http')) return;
// Only attempt to send a message if content script is loaded
chrome.tabs.sendMessage(tab.id, { type: 'toggleSidebar' }).catch(() => {
// silently ignore "Receiving end does not exist"
});
}).catch(console.error);
}
});