injected-controls.ts RawHistoryFolder
1/**
2 * Injectable Controls Script
3 *
4 * This script is injected into every page loaded in the browser to provide
5 * global keyboard and mouse controls that work on all domains.
6 *
7 * Only includes essential controls that can work cross-origin:
8 * - Navigation (back/forward)
9 * - Mouse navigation buttons
10 *
11 * Zoom controls are handled natively by the GTK backend
12 */
13
14(function() {
15 'use strict';
16
17 // Prevent multiple injections with a unique timestamp
18 const injectionId = '__dumberControlsInjected_' + Date.now();
19 if ((window as any).__dumberControlsInjected) {
20 console.log('🔄 Dumber Browser controls already injected, skipping');
21 return;
22 }
23 (window as any).__dumberControlsInjected = injectionId;
24
25 console.log('🚀 Dumber Browser controls injected on:', window.location.href);
26
27 // Keyboard event handler for global controls
28 function handleKeyboardEvent(event: KeyboardEvent): void {
29 const { ctrlKey, metaKey, shiftKey, altKey, key } = event;
30 const isCmd = ctrlKey || metaKey;
31
32 try {
33 // Navigation: Alt + Left Arrow (Back)
34 if (altKey && key === 'ArrowLeft') {
35 event.preventDefault();
36 event.stopPropagation();
37
38 if (window.history.length > 1) {
39 console.log('⬅️ Navigating back');
40 window.history.back();
41 }
42 return;
43 }
44
45 // Navigation: Alt + Right Arrow (Forward)
46 if (altKey && key === 'ArrowRight') {
47 event.preventDefault();
48 event.stopPropagation();
49
50 console.log('➡️ Navigating forward');
51 window.history.forward();
52 return;
53 }
54
55 } catch (error) {
56 console.error('❌ Error in keyboard handler:', error);
57 }
58 }
59
60 // Mouse event handler for navigation buttons
61 function handleMouseEvent(event: MouseEvent): void {
62 try {
63 // Mouse button 3 (back button)
64 if (event.button === 3) {
65 event.preventDefault();
66 event.stopPropagation();
67
68 if (window.history.length > 1) {
69 console.log('⬅️ Mouse back button pressed');
70 window.history.back();
71 }
72 return;
73 }
74
75 // Mouse button 4 (forward button)
76 if (event.button === 4) {
77 event.preventDefault();
78 event.stopPropagation();
79
80 console.log('➡️ Mouse forward button pressed');
81 window.history.forward();
82 return;
83 }
84
85 } catch (error) {
86 console.error('❌ Error in mouse handler:', error);
87 }
88 }
89
90 // Add event listeners with high priority (capture phase)
91 try {
92 document.addEventListener('keydown', handleKeyboardEvent, true);
93 document.addEventListener('mousedown', handleMouseEvent, true);
94
95 console.log('✅ Dumber Browser global controls active');
96
97 // Cleanup function for when page is unloaded
98 window.addEventListener('beforeunload', () => {
99 document.removeEventListener('keydown', handleKeyboardEvent, true);
100 document.removeEventListener('mousedown', handleMouseEvent, true);
101 console.log('🧹 Dumber Browser controls cleaned up');
102 });
103
104 } catch (error) {
105 console.error('❌ Failed to initialize Dumber Browser controls:', error);
106 }
107})();