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 * - URL copying
10 * - Mouse navigation buttons
11 *
12 * Zoom controls are handled natively by the GTK backend
13 */
14
15(function() {
16 'use strict';
17
18 // Prevent multiple injections with a unique timestamp
19 const injectionId = '__dumberControlsInjected_' + Date.now();
20 if ((window as any).__dumberControlsInjected) {
21 console.log('🔄 Dumber Browser controls already injected, skipping');
22 return;
23 }
24 (window as any).__dumberControlsInjected = injectionId;
25
26 console.log('🚀 Dumber Browser controls injected on:', window.location.href);
27
28 // Keyboard event handler for global controls
29 function handleKeyboardEvent(event: KeyboardEvent): void {
30 const { ctrlKey, metaKey, shiftKey, altKey, key } = event;
31 const isCmd = ctrlKey || metaKey;
32
33 try {
34 // Copy URL (Cmd+Shift+C)
35 if (isCmd && shiftKey && key === 'C') {
36 event.preventDefault();
37 event.stopPropagation();
38
39 // Copy current URL to clipboard
40 if (navigator.clipboard && navigator.clipboard.writeText) {
41 navigator.clipboard.writeText(window.location.href)
42 .then(() => console.log('🔗 URL copied:', window.location.href))
43 .catch((err) => console.error('❌ Failed to copy URL:', err));
44 } else {
45 console.warn('⚠️ Clipboard API not available');
46 }
47 return;
48 }
49
50 // Navigation: Alt + Left Arrow (Back)
51 if (altKey && key === 'ArrowLeft') {
52 event.preventDefault();
53 event.stopPropagation();
54
55 if (window.history.length > 1) {
56 console.log('⬅️ Navigating back');
57 window.history.back();
58 }
59 return;
60 }
61
62 // Navigation: Alt + Right Arrow (Forward)
63 if (altKey && key === 'ArrowRight') {
64 event.preventDefault();
65 event.stopPropagation();
66
67 console.log('➡️ Navigating forward');
68 window.history.forward();
69 return;
70 }
71
72 } catch (error) {
73 console.error('❌ Error in keyboard handler:', error);
74 }
75 }
76
77 // Mouse event handler for navigation buttons
78 function handleMouseEvent(event: MouseEvent): void {
79 try {
80 // Mouse button 3 (back button)
81 if (event.button === 3) {
82 event.preventDefault();
83 event.stopPropagation();
84
85 if (window.history.length > 1) {
86 console.log('⬅️ Mouse back button pressed');
87 window.history.back();
88 }
89 return;
90 }
91
92 // Mouse button 4 (forward button)
93 if (event.button === 4) {
94 event.preventDefault();
95 event.stopPropagation();
96
97 console.log('➡️ Mouse forward button pressed');
98 window.history.forward();
99 return;
100 }
101
102 } catch (error) {
103 console.error('❌ Error in mouse handler:', error);
104 }
105 }
106
107 // Add event listeners with high priority (capture phase)
108 try {
109 document.addEventListener('keydown', handleKeyboardEvent, true);
110 document.addEventListener('mousedown', handleMouseEvent, true);
111
112 console.log('✅ Dumber Browser global controls active');
113
114 // Cleanup function for when page is unloaded
115 window.addEventListener('beforeunload', () => {
116 document.removeEventListener('keydown', handleKeyboardEvent, true);
117 document.removeEventListener('mousedown', handleMouseEvent, true);
118 console.log('🧹 Dumber Browser controls cleaned up');
119 });
120
121 } catch (error) {
122 console.error('❌ Failed to initialize Dumber Browser controls:', error);
123 }
124})();