-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
749 lines (655 loc) · 26.6 KB
/
content.js
File metadata and controls
749 lines (655 loc) · 26.6 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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
// Meet Default Off - Content Script
// Handles keyboard shortcuts for camera and microphone control
console.log('🎬 Meet Default Off content script loaded');
// Detect operating system
function detectOS() {
const userAgent = navigator.userAgent.toLowerCase();
if (userAgent.includes('mac')) {
return 'mac';
} else if (userAgent.includes('win')) {
return 'windows';
} else if (userAgent.includes('linux')) {
return 'linux';
}
return 'windows'; // Default to Windows shortcuts
}
// Get correct keyboard shortcuts based on OS
function getShortcuts(os) {
switch (os) {
case 'mac':
return {
video: { key: 'e', metaKey: true }, // Cmd+E
audio: { key: 'd', metaKey: true } // Cmd+D
};
case 'windows':
case 'linux':
default:
return {
video: { key: 'e', ctrlKey: true }, // Ctrl+E
audio: { key: 'd', ctrlKey: true } // Ctrl+D
};
}
}
// Store original getUserMedia function
const originalGetUserMedia = navigator.mediaDevices.getUserMedia;
// Current settings
let currentSettings = {
camera: 'block',
microphone: 'block',
blockingMethod: 'permissions'
};
// Method isolation - ensure only one method is active
let activeMethod = null;
// Override getUserMedia based on user settings
async function overrideGetUserMedia(constraints) {
// Only block if using permission-based method and user wants to block
// Keyboard method should NOT interfere with getUserMedia at all
if (currentSettings.blockingMethod === 'permissions') {
const shouldBlockVideo = constraints.video && currentSettings.camera === 'block';
const shouldBlockAudio = constraints.audio && currentSettings.microphone === 'block';
if (shouldBlockVideo || shouldBlockAudio) {
console.log('🚫 Blocked camera/mic request by Meet Default Off (permissions method):', {
video: shouldBlockVideo,
audio: shouldBlockAudio
});
return Promise.reject(new DOMException('Blocked by Meet Default Off extension'));
}
}
// For keyboard method, always allow getUserMedia - we'll control via keyboard shortcuts
console.log('✅ Allowing getUserMedia request (keyboard method or permissions allow)');
return originalGetUserMedia.call(navigator.mediaDevices, constraints);
}
// Apply the override
navigator.mediaDevices.getUserMedia = overrideGetUserMedia;
// Update current settings
function updateSettings(camera, microphone, blockingMethod) {
currentSettings = { camera, microphone, blockingMethod };
console.log('🔄 Settings updated in content script:', currentSettings);
}
// Debounce function to prevent rapid repeated executions
let permissionSettingsTimeout = null;
let lastPermissionSettings = null;
// Check if Google Meet is showing a permission dialog
function isPermissionDialogOpen() {
// Look for common Google Meet permission dialog elements
const dialogSelectors = [
'[role="dialog"]',
'.dialog',
'[aria-modal="true"]',
'[data-dialog]',
'.permission-dialog',
'[aria-label*="camera"]',
'[aria-label*="microphone"]'
];
for (const selector of dialogSelectors) {
const dialog = document.querySelector(selector);
if (dialog && dialog.offsetParent !== null) {
return true;
}
}
return false;
}
// Apply permission-based settings (enable/disable camera and mic)
function applyPermissionSettings(camera, microphone) {
// Method isolation - only run if this is the active method
if (activeMethod && activeMethod !== 'permissions') {
console.log('🔐 Skipping permission settings - keyboard method is active');
return;
}
activeMethod = 'permissions';
const os = detectOS();
const shortcuts = getShortcuts(os);
// Debounce: if same settings are being applied, skip
const currentSettings = `${camera}-${microphone}`;
if (lastPermissionSettings === currentSettings) {
console.log('🔐 Skipping duplicate permission settings:', { camera, microphone });
return;
}
lastPermissionSettings = currentSettings;
// Clear any existing timeout
if (permissionSettingsTimeout) {
clearTimeout(permissionSettingsTimeout);
}
console.log(`🔐 Applying permission settings:`, { camera, microphone });
// Single execution with proper timing
permissionSettingsTimeout = setTimeout(() => {
try {
// Check if Google Meet is showing a permission dialog - if so, don't interfere
if (isPermissionDialogOpen()) {
console.log('🚫 Google Meet permission dialog is open, skipping auto-enable to avoid interference');
return;
}
// Only try to enable if user wants to allow camera/mic
if (camera === 'allow' || microphone === 'allow') {
// Method 1: Try to find and click Google Meet buttons (more conservative)
const enableCameraAndMicButtons = () => {
// Look for camera button (video toggle) - more specific selectors
if (camera === 'allow') {
const cameraButton = document.querySelector('[aria-label*="Turn on camera"]:not([aria-label*="Turn off camera"])');
if (cameraButton && cameraButton.offsetParent !== null) { // Check if visible
cameraButton.click();
console.log('📹 Clicked camera button to enable');
}
}
// Look for microphone button (audio toggle) - more specific selectors
if (microphone === 'allow') {
const micButton = document.querySelector('[aria-label*="Turn on microphone"]:not([aria-label*="Turn off microphone"])');
if (micButton && micButton.offsetParent !== null) { // Check if visible
micButton.click();
console.log('🎤 Clicked microphone button to enable');
}
}
};
// Only use button clicking for permissions method - no keyboard shortcuts
// Keyboard shortcuts are handled separately by the keyboard method
enableCameraAndMicButtons();
}
} catch (error) {
console.error('❌ Error applying permission settings:', error);
}
}, 500); // Reduced delay for faster response
}
// Immediate version for user-initiated changes (no delay)
function applyPermissionSettingsImmediate(camera, microphone) {
// Method isolation - only run if this is the active method
if (activeMethod && activeMethod !== 'permissions') {
console.log('🔐 Skipping permission settings - keyboard method is active');
return;
}
activeMethod = 'permissions';
console.log(`🔐 Applying permission settings immediately:`, { camera, microphone });
try {
// Check if Google Meet is showing a permission dialog - if so, don't interfere
if (isPermissionDialogOpen()) {
console.log('🚫 Google Meet permission dialog is open, skipping auto-enable to avoid interference');
return;
}
// Only try to enable if user wants to allow camera/mic
if (camera === 'allow' || microphone === 'allow') {
// Look for camera button (video toggle) - more specific selectors
if (camera === 'allow') {
const cameraButton = document.querySelector('[aria-label*="Turn on camera"]:not([aria-label*="Turn off camera"])');
if (cameraButton && cameraButton.offsetParent !== null) { // Check if visible
cameraButton.click();
console.log('📹 Clicked camera button to enable (immediate)');
}
}
// Look for microphone button (audio toggle) - more specific selectors
if (microphone === 'allow') {
const micButton = document.querySelector('[aria-label*="Turn on microphone"]:not([aria-label*="Turn off microphone"])');
if (micButton && micButton.offsetParent !== null) { // Check if visible
micButton.click();
console.log('🎤 Clicked microphone button to enable (immediate)');
}
}
}
} catch (error) {
console.error('❌ Error applying permission settings immediately:', error);
}
}
// Debounce for keyboard settings
let keyboardSettingsTimeout = null;
let lastKeyboardSettings = null;
// Detect current camera and microphone state in Google Meet (improved)
function detectCurrentMediaState() {
const state = { camera: 'unknown', microphone: 'unknown' };
try {
console.log('🔍 Detecting media state in Google Meet...');
// Look for camera button state with multiple selectors
const cameraSelectors = [
'[aria-label*="Turn off camera"]',
'[aria-label*="Turn on camera"]',
'[data-is-muted="true"]',
'[data-is-muted="false"]',
'[jsname="BOHaEe"]',
'button[aria-label*="camera"]',
'button[aria-label*="video"]'
];
for (const selector of cameraSelectors) {
const cameraButton = document.querySelector(selector);
if (cameraButton) {
const ariaLabel = cameraButton.getAttribute('aria-label') || '';
const dataMuted = cameraButton.getAttribute('data-is-muted');
const isPressed = cameraButton.getAttribute('aria-pressed');
console.log(`📹 Found camera button:`, { ariaLabel, dataMuted, isPressed });
if (ariaLabel.includes('Turn off camera') || dataMuted === 'false' || isPressed === 'true') {
state.camera = 'on';
break;
} else if (ariaLabel.includes('Turn on camera') || dataMuted === 'true' || isPressed === 'false') {
state.camera = 'off';
break;
}
}
}
// Look for microphone button state with multiple selectors
const micSelectors = [
'[aria-label*="Turn off microphone"]',
'[aria-label*="Turn on microphone"]',
'[data-is-muted="true"]',
'[data-is-muted="false"]',
'[jsname="BOHaEe"]',
'button[aria-label*="microphone"]',
'button[aria-label*="audio"]'
];
for (const selector of micSelectors) {
const micButton = document.querySelector(selector);
if (micButton) {
const ariaLabel = micButton.getAttribute('aria-label') || '';
const dataMuted = micButton.getAttribute('data-is-muted');
const isPressed = micButton.getAttribute('aria-pressed');
console.log(`🎤 Found mic button:`, { ariaLabel, dataMuted, isPressed });
if (ariaLabel.includes('Turn off microphone') || dataMuted === 'false' || isPressed === 'true') {
state.microphone = 'on';
break;
} else if (ariaLabel.includes('Turn on microphone') || dataMuted === 'true' || isPressed === 'false') {
state.microphone = 'off';
break;
}
}
}
// Additional fallback: look for visual indicators
if (state.camera === 'unknown') {
// Look for camera icon states
const cameraIcon = document.querySelector('svg[aria-label*="camera"], svg[aria-label*="video"]');
if (cameraIcon) {
const iconPath = cameraIcon.querySelector('path');
if (iconPath) {
const pathData = iconPath.getAttribute('d') || '';
// Google Meet uses different path data for on/off states
if (pathData.includes('M12') || pathData.includes('M6')) {
state.camera = 'off'; // Common pattern for "off" state
} else {
state.camera = 'on';
}
}
}
}
if (state.microphone === 'unknown') {
// Look for microphone icon states
const micIcon = document.querySelector('svg[aria-label*="microphone"], svg[aria-label*="audio"]');
if (micIcon) {
const iconPath = micIcon.querySelector('path');
if (iconPath) {
const pathData = iconPath.getAttribute('d') || '';
if (pathData.includes('M12') || pathData.includes('M6')) {
state.microphone = 'off'; // Common pattern for "off" state
} else {
state.microphone = 'on';
}
}
}
}
} catch (error) {
console.error('❌ Error detecting media state:', error);
}
console.log('🔍 Detected current media state:', state);
return state;
}
// Send keyboard shortcut with improved targeting for Google Meet
function sendKeyboardShortcut(key, ctrlKey, metaKey, description) {
try {
console.log(`⌨️ Attempting to send ${description} shortcut:`, { key, ctrlKey, metaKey });
// Create a more realistic keyboard event that Google Meet will recognize
const createKeyboardEvent = (target) => {
return new KeyboardEvent('keydown', {
key: key,
code: key === 'e' ? 'KeyE' : 'KeyD', // Add proper key code
ctrlKey: ctrlKey || false,
metaKey: metaKey || false,
shiftKey: false,
altKey: false,
bubbles: true,
cancelable: true,
composed: true,
isTrusted: false, // We'll try both trusted and non-trusted events
charCode: 0,
keyCode: key === 'e' ? 69 : 68, // Add keyCode for compatibility
which: key === 'e' ? 69 : 68
});
};
let successCount = 0;
// Method 1: Dispatch to window with trusted event simulation
try {
const event1 = createKeyboardEvent(window);
window.dispatchEvent(event1);
successCount++;
console.log(`✅ Dispatched ${description} to window`);
} catch (e) {
console.log(`❌ Failed to dispatch to window:`, e);
}
// Method 2: Dispatch to document
try {
const event2 = createKeyboardEvent(document);
document.dispatchEvent(event2);
successCount++;
console.log(`✅ Dispatched ${description} to document`);
} catch (e) {
console.log(`❌ Failed to dispatch to document:`, e);
}
// Method 3: Dispatch to body element
try {
const event3 = createKeyboardEvent(document.body);
document.body.dispatchEvent(event3);
successCount++;
console.log(`✅ Dispatched ${description} to body`);
} catch (e) {
console.log(`❌ Failed to dispatch to body:`, e);
}
// Method 4: Try to find Google Meet's main container and dispatch there
try {
const meetContainer = document.querySelector('[data-meeting-title], .crqnQb, [jsname="BOHaEe"]') ||
document.querySelector('body > div') ||
document.querySelector('#yDmH0d');
if (meetContainer) {
const event4 = createKeyboardEvent(meetContainer);
meetContainer.dispatchEvent(event4);
successCount++;
console.log(`✅ Dispatched ${description} to Meet container`);
}
} catch (e) {
console.log(`❌ Failed to dispatch to Meet container:`, e);
}
// Method 5: Try simulating a real user interaction
try {
// Focus on the document first
document.body.focus();
// Create and dispatch keydown
const keydownEvent = createKeyboardEvent(document);
document.dispatchEvent(keydownEvent);
// Create and dispatch keyup
const keyupEvent = new KeyboardEvent('keyup', {
key: key,
code: key === 'e' ? 'KeyE' : 'KeyD',
ctrlKey: ctrlKey || false,
metaKey: metaKey || false,
shiftKey: false,
altKey: false,
bubbles: true,
cancelable: true,
composed: true
});
document.dispatchEvent(keyupEvent);
successCount++;
console.log(`✅ Dispatched ${description} keydown + keyup sequence`);
} catch (e) {
console.log(`❌ Failed keydown + keyup sequence:`, e);
}
console.log(`⌨️ Sent ${description} shortcut (${key}) - ${successCount} methods succeeded`);
return successCount > 0;
} catch (error) {
console.error(`❌ Error sending ${description} shortcut:`, error);
return false;
}
}
// Apply keyboard shortcuts based on settings
function applyKeyboardSettings(camera, microphone) {
// Method isolation - only run if this is the active method
if (activeMethod && activeMethod !== 'keyboard') {
console.log('⌨️ Skipping keyboard settings - permissions method is active');
return;
}
activeMethod = 'keyboard';
const os = detectOS();
const shortcuts = getShortcuts(os);
// Debounce: if same settings are being applied, skip
const currentSettings = `${camera}-${microphone}`;
if (lastKeyboardSettings === currentSettings) {
console.log('⌨️ Skipping duplicate keyboard settings:', { camera, microphone });
return;
}
lastKeyboardSettings = currentSettings;
// Clear any existing timeout
if (keyboardSettingsTimeout) {
clearTimeout(keyboardSettingsTimeout);
}
console.log(`⌨️ Applying keyboard shortcuts for ${os}:`, { camera, microphone });
// Single execution with proper timing
keyboardSettingsTimeout = setTimeout(() => {
try {
// Detect current state
const currentState = detectCurrentMediaState();
// Apply camera setting
const desiredCameraState = camera === 'allow' ? 'on' : 'off';
if (currentState.camera !== desiredCameraState && currentState.camera !== 'unknown') {
console.log(`📹 Camera state: ${currentState.camera} → ${desiredCameraState}`);
const success = sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle'
);
// Retry if first attempt failed
if (!success) {
setTimeout(() => {
sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle (retry)'
);
}, 200);
}
}
// Apply microphone setting
const desiredMicState = microphone === 'allow' ? 'on' : 'off';
if (currentState.microphone !== desiredMicState && currentState.microphone !== 'unknown') {
console.log(`🎤 Microphone state: ${currentState.microphone} → ${desiredMicState}`);
const success = sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle'
);
// Retry if first attempt failed
if (!success) {
setTimeout(() => {
sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle (retry)'
);
}, 200);
}
}
// If we couldn't detect state, try toggling anyway (fallback)
// For keyboard method, be more aggressive with fallbacks
if (currentState.camera === 'unknown') {
if (camera === 'block') {
console.log('📹 Unknown camera state, sending toggle shortcut to turn OFF');
sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle OFF (fallback)'
);
} else if (camera === 'allow') {
console.log('📹 Unknown camera state, sending toggle shortcut to turn ON');
sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle ON (fallback)'
);
}
}
if (currentState.microphone === 'unknown') {
if (microphone === 'block') {
console.log('🎤 Unknown microphone state, sending toggle shortcut to turn OFF');
sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle OFF (fallback)'
);
} else if (microphone === 'allow') {
console.log('🎤 Unknown microphone state, sending toggle shortcut to turn ON');
sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle ON (fallback)'
);
}
}
// Additional fallback: Try direct button clicking if keyboard shortcuts fail
setTimeout(() => {
console.log('🔄 Checking if keyboard shortcuts worked, trying button clicking as fallback...');
const newState = detectCurrentMediaState();
const cameraStillNeedsToggle = (camera === 'allow' && newState.camera === 'off') ||
(camera === 'block' && newState.camera === 'on');
const micStillNeedsToggle = (microphone === 'allow' && newState.microphone === 'off') ||
(microphone === 'block' && newState.microphone === 'on');
if (cameraStillNeedsToggle) {
console.log('📹 Keyboard shortcut didn\'t work for camera, trying button click');
const cameraButton = document.querySelector('[aria-label*="Turn on camera"], [aria-label*="Turn off camera"]');
if (cameraButton) {
cameraButton.click();
console.log('📹 Clicked camera button as fallback');
}
}
if (micStillNeedsToggle) {
console.log('🎤 Keyboard shortcut didn\'t work for microphone, trying button click');
const micButton = document.querySelector('[aria-label*="Turn on microphone"], [aria-label*="Turn off microphone"]');
if (micButton) {
micButton.click();
console.log('🎤 Clicked microphone button as fallback');
}
}
}, 1000); // Reduced delay for faster fallback
} catch (error) {
console.error('❌ Error applying keyboard shortcuts:', error);
}
}, 500); // Reduced delay for faster response
}
// Immediate version for user-initiated changes (no delay)
function applyKeyboardSettingsImmediate(camera, microphone) {
// Method isolation - only run if this is the active method
if (activeMethod && activeMethod !== 'keyboard') {
console.log('⌨️ Skipping keyboard settings - permissions method is active');
return;
}
activeMethod = 'keyboard';
const os = detectOS();
const shortcuts = getShortcuts(os);
console.log(`⌨️ Applying keyboard shortcuts immediately for ${os}:`, { camera, microphone });
try {
// Detect current state
const currentState = detectCurrentMediaState();
// Apply camera setting
const desiredCameraState = camera === 'allow' ? 'on' : 'off';
if (currentState.camera !== desiredCameraState && currentState.camera !== 'unknown') {
console.log(`📹 Camera state: ${currentState.camera} → ${desiredCameraState} (immediate)`);
sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle (immediate)'
);
}
// Apply microphone setting
const desiredMicState = microphone === 'allow' ? 'on' : 'off';
if (currentState.microphone !== desiredMicState && currentState.microphone !== 'unknown') {
console.log(`🎤 Microphone state: ${currentState.microphone} → ${desiredMicState} (immediate)`);
sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle (immediate)'
);
}
// If we couldn't detect state, try toggling anyway (fallback)
if (currentState.camera === 'unknown') {
if (camera === 'block') {
console.log('📹 Unknown camera state, sending toggle shortcut to turn OFF (immediate)');
sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle OFF (immediate fallback)'
);
} else if (camera === 'allow') {
console.log('📹 Unknown camera state, sending toggle shortcut to turn ON (immediate)');
sendKeyboardShortcut(
shortcuts.video.key,
shortcuts.video.ctrlKey,
shortcuts.video.metaKey,
'video toggle ON (immediate fallback)'
);
}
}
if (currentState.microphone === 'unknown') {
if (microphone === 'block') {
console.log('🎤 Unknown microphone state, sending toggle shortcut to turn OFF (immediate)');
sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle OFF (immediate fallback)'
);
} else if (microphone === 'allow') {
console.log('🎤 Unknown microphone state, sending toggle shortcut to turn ON (immediate)');
sendKeyboardShortcut(
shortcuts.audio.key,
shortcuts.audio.ctrlKey,
shortcuts.audio.metaKey,
'audio toggle ON (immediate fallback)'
);
}
}
} catch (error) {
console.error('❌ Error applying keyboard shortcuts immediately:', error);
}
}
// Listen for messages from background script
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'applyKeyboardSettings') {
console.log('📨 Received keyboard settings:', message);
updateSettings(message.camera, message.microphone, 'keyboard');
// Apply immediately for user-initiated changes
applyKeyboardSettingsImmediate(message.camera, message.microphone);
sendResponse({ success: true });
} else if (message.action === 'applyPermissionSettings') {
console.log('📨 Received permission settings:', message);
updateSettings(message.camera, message.microphone, 'permissions');
// Apply immediately for user-initiated changes
applyPermissionSettingsImmediate(message.camera, message.microphone);
sendResponse({ success: true });
}
});
// Auto-apply settings when page loads (with debouncing)
let pageLoadTimeout = null;
let hasAppliedSettings = false;
window.addEventListener('load', async () => {
// Prevent multiple executions
if (hasAppliedSettings) {
console.log('🔄 Settings already applied on page load, skipping');
return;
}
// Clear any existing timeout
if (pageLoadTimeout) {
clearTimeout(pageLoadTimeout);
}
pageLoadTimeout = setTimeout(async () => {
try {
const result = await chrome.storage.sync.get({
camera: 'block',
microphone: 'block',
blockingMethod: 'permissions'
});
// Update current settings
updateSettings(result.camera, result.microphone, result.blockingMethod);
if (result.blockingMethod === 'keyboard') {
console.log('🔄 Auto-applying keyboard settings on page load');
applyKeyboardSettings(result.camera, result.microphone);
} else {
console.log('🔄 Auto-applying permission settings on page load');
applyPermissionSettings(result.camera, result.microphone);
}
hasAppliedSettings = true;
} catch (error) {
console.error('❌ Error auto-applying settings:', error);
}
}, 1000); // Reduced delay for faster page load response
});
console.log('✅ Meet Default Off content script ready');