-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
210 lines (180 loc) · 7.08 KB
/
popup.js
File metadata and controls
210 lines (180 loc) · 7.08 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Default settings
const DEFAULT_SETTINGS = {
camera: 'block',
microphone: 'block',
blockingMethod: 'permissions'
};
// Load settings from storage and populate UI
async function loadSettings() {
try {
const result = await chrome.storage.sync.get(DEFAULT_SETTINGS);
// Set camera setting
const cameraRadio = document.getElementById(`camera-${result.camera}`);
if (cameraRadio) {
cameraRadio.checked = true;
}
// Set microphone setting
const micRadio = document.getElementById(`mic-${result.microphone}`);
if (micRadio) {
micRadio.checked = true;
}
// Set blocking method
const methodRadio = document.getElementById(`method-${result.blockingMethod}`);
if (methodRadio) {
methodRadio.checked = true;
}
// Update badge to reflect current settings
await updateBadge(result.camera, result.microphone);
showStatus('Settings loaded successfully', false);
} catch (error) {
console.error('Error loading settings:', error);
showStatus('Error loading settings', true);
}
}
// Update badge to show current settings
async function updateBadge(camera, microphone) {
try {
const cameraState = camera === 'allow' ? 'on' : 'off';
const micState = microphone === 'allow' ? 'on' : 'off';
let badgeText = '';
let badgeColor = '#e74c3c';
if (cameraState === 'off' && micState === 'off') {
badgeText = '🚫';
badgeColor = '#e74c3c';
} else if (cameraState === 'off' && micState === 'on') {
badgeText = '🎤';
badgeColor = '#f39c12';
} else if (cameraState === 'on' && micState === 'off') {
badgeText = '📹';
badgeColor = '#3498db';
} else if (cameraState === 'on' && micState === 'on') {
badgeText = '✅';
badgeColor = '#27ae60';
}
await chrome.action.setBadgeText({ text: badgeText });
await chrome.action.setBadgeBackgroundColor({ color: badgeColor });
} catch (error) {
console.error('Error updating badge from popup:', error);
}
}
// Save settings to storage
async function saveSettings(camera, microphone, blockingMethod) {
try {
await chrome.storage.sync.set({
camera: camera,
microphone: microphone,
blockingMethod: blockingMethod
});
// Apply settings immediately (this will also update the badge)
await applySettings(camera, microphone, blockingMethod);
// Also update badge immediately from popup
await updateBadge(camera, microphone);
showStatus('Settings saved and applied!', false);
} catch (error) {
console.error('Error saving settings:', error);
showStatus('Error saving settings', true);
}
}
// Apply settings to content settings
async function applySettings(camera, microphone, blockingMethod) {
try {
if (blockingMethod === 'permissions') {
// Clear any existing keyboard method settings first
console.log('🔐 Switching to permissions method - clearing keyboard method');
// Apply camera setting
await chrome.contentSettings.camera.set({
primaryPattern: "https://meet.google.com/*",
setting: camera
});
// Apply microphone setting
await chrome.contentSettings.microphone.set({
primaryPattern: "https://meet.google.com/*",
setting: microphone
});
// Also send to content script for getUserMedia override
const tabs = await chrome.tabs.query({ url: "https://meet.google.com/*" });
for (const tab of tabs) {
try {
await chrome.tabs.sendMessage(tab.id, {
action: 'applyPermissionSettings',
camera: camera,
microphone: microphone
});
console.log(`📨 Permission settings sent to tab ${tab.id}`);
} catch (error) {
console.log(`⚠️ Could not send permission message to tab ${tab.id}:`, error.message);
}
}
console.log(`✅ Permission settings applied: Camera=${camera}, Microphone=${microphone}`);
} else if (blockingMethod === 'keyboard') {
// Keyboard method - NO browser permissions, only keyboard shortcuts
console.log('⌨️ Switching to keyboard method - using ONLY keyboard shortcuts');
// Don't set any browser permissions - let Google Meet handle permissions naturally
// The keyboard method will use Ctrl+E/D to toggle camera/mic based on current state
// Send message to content script for keyboard shortcuts
const tabs = await chrome.tabs.query({ url: "https://meet.google.com/*" });
for (const tab of tabs) {
try {
await chrome.tabs.sendMessage(tab.id, {
action: 'applyKeyboardSettings',
camera: camera,
microphone: microphone
});
console.log(`📨 Keyboard settings sent to tab ${tab.id}`);
} catch (error) {
console.log(`⚠️ Could not send message to tab ${tab.id}:`, error.message);
}
}
console.log(`✅ Keyboard shortcut settings applied: Camera=${camera}, Microphone=${microphone}`);
}
} catch (error) {
console.error('Error applying settings:', error);
throw error;
}
}
// Show status message
function showStatus(message, isError = false) {
const statusDiv = document.getElementById('status');
statusDiv.textContent = message;
statusDiv.className = isError ? 'status error' : 'status';
statusDiv.style.display = 'block';
// Hide status after 3 seconds
setTimeout(() => {
statusDiv.style.display = 'none';
}, 3000);
}
// Handle radio button changes
function setupEventListeners() {
// Camera setting change
document.querySelectorAll('input[name="camera"]').forEach(radio => {
radio.addEventListener('change', async (e) => {
const camera = e.target.value;
const micSetting = document.querySelector('input[name="microphone"]:checked').value;
const blockingMethod = document.querySelector('input[name="blockingMethod"]:checked').value;
await saveSettings(camera, micSetting, blockingMethod);
});
});
// Microphone setting change
document.querySelectorAll('input[name="microphone"]').forEach(radio => {
radio.addEventListener('change', async (e) => {
const microphone = e.target.value;
const cameraSetting = document.querySelector('input[name="camera"]:checked').value;
const blockingMethod = document.querySelector('input[name="blockingMethod"]:checked').value;
await saveSettings(cameraSetting, microphone, blockingMethod);
});
});
// Blocking method change
document.querySelectorAll('input[name="blockingMethod"]').forEach(radio => {
radio.addEventListener('change', async (e) => {
const blockingMethod = e.target.value;
const cameraSetting = document.querySelector('input[name="camera"]:checked').value;
const micSetting = document.querySelector('input[name="microphone"]:checked').value;
await saveSettings(cameraSetting, micSetting, blockingMethod);
});
});
}
// Initialize popup when DOM is loaded
document.addEventListener('DOMContentLoaded', () => {
loadSettings();
setupEventListeners();
});