-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
56 lines (48 loc) · 1.61 KB
/
popup.js
File metadata and controls
56 lines (48 loc) · 1.61 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
// Toggle highlighting on/off
document.getElementById('highlightToggle').addEventListener('click', async function() {
const toggle = this;
const isActive = toggle.classList.contains('active');
// Toggle the UI
toggle.classList.toggle('active');
// Send message to content script to toggle highlighting
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab?.id) {
chrome.tabs.sendMessage(tab.id, {
action: 'toggleHighlighting',
enabled: !isActive
});
}
});
// Get initial state when popup opens
(async function() {
// First try to get state from storage
chrome.storage.local.get('highlightingEnabled', (result) => {
const toggle = document.getElementById('highlightToggle');
const enabled = result.highlightingEnabled !== false; // Default to true
if (enabled) {
toggle.classList.add('active');
} else {
toggle.classList.remove('active');
}
});
// Also try to sync with current tab's content script
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
if (tab?.id) {
chrome.tabs.sendMessage(tab.id, {
action: 'getHighlightingState'
}, (response) => {
if (chrome.runtime.lastError) {
// Ignore errors (e.g., when on restricted pages)
return;
}
if (response && typeof response.enabled === 'boolean') {
const toggle = document.getElementById('highlightToggle');
if (response.enabled) {
toggle.classList.add('active');
} else {
toggle.classList.remove('active');
}
}
});
}
})();