forked from kfurgol/mac-cmd-scroll
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
78 lines (68 loc) · 2.69 KB
/
popup.js
File metadata and controls
78 lines (68 loc) · 2.69 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
76
77
78
document.addEventListener('DOMContentLoaded', async () => {
const powerButton = document.getElementById('powerToggle');
const directionToggle = document.getElementById('directionToggle');
// Load current state
const { extensionEnabled = true, scrollDirection = 'normal' } = await chrome.storage.sync.get(['extensionEnabled', 'scrollDirection']);
updateButtonState(extensionEnabled);
updateDirectionState(scrollDirection);
// Handle power toggle - click on power button area
powerButton.addEventListener('click', async (e) => {
e.stopPropagation();
const { extensionEnabled = true } = await chrome.storage.sync.get('extensionEnabled');
const newState = !extensionEnabled;
await chrome.storage.sync.set({ extensionEnabled: newState });
updateButtonState(newState);
// Notify all tabs about the state change
const tabs = await chrome.tabs.query({});
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, {
action: 'toggleExtension',
enabled: newState
}).catch(() => {
// Ignore errors for tabs where content script isn't loaded
});
});
});
// Handle direction toggle
directionToggle.addEventListener('click', async (e) => {
e.stopPropagation();
const { scrollDirection = 'normal' } = await chrome.storage.sync.get('scrollDirection');
const newDirection = scrollDirection === 'normal' ? 'inverted' : 'normal';
await chrome.storage.sync.set({ scrollDirection: newDirection });
updateDirectionState(newDirection);
// Notify all tabs about the direction change
const tabs = await chrome.tabs.query({});
tabs.forEach(tab => {
chrome.tabs.sendMessage(tab.id, {
action: 'updateScrollDirection',
direction: newDirection
}).catch(() => {
// Ignore errors for tabs where content script isn't loaded
});
});
});
function updateButtonState(enabled) {
const statusText = document.getElementById('statusText');
if (enabled) {
powerButton.classList.add('enabled');
powerButton.classList.remove('disabled');
statusText.classList.add('enabled');
statusText.classList.remove('disabled');
statusText.textContent = 'Turn off';
} else {
powerButton.classList.add('disabled');
powerButton.classList.remove('enabled');
statusText.classList.add('disabled');
statusText.classList.remove('enabled');
statusText.textContent = 'Turn on';
}
}
function updateDirectionState(direction) {
const directionText = document.getElementById('directionText');
if (direction === 'normal') {
directionText.innerHTML = '↓ Zoom In';
} else {
directionText.innerHTML = '↓ Zoom Out';
}
}
});