-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontentScript.js
More file actions
174 lines (143 loc) · 6.29 KB
/
contentScript.js
File metadata and controls
174 lines (143 loc) · 6.29 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
let speechRecognition = null;
let shouldRestart = true;
let lastResponseTime = Date.now();
// Create a stop button
const stopBtn = document.createElement('button');
stopBtn.className = 'stop-recording-button';
stopBtn.innerHTML = `
<svg width="24" height="24" viewBox="0 0 24 24" style="margin-right:8px;" fill="none" xmlns="http://www.w3.org/2000/svg">
<path fill-rule="evenodd" clip-rule="evenodd" d="M2.25 12C2.25 6.61522 6.61522 2.25 12 2.25C17.3848 2.25 21.75 6.61522 21.75 12C21.75 17.3848 17.3848 21.75 12 21.75C6.61522 21.75 2.25 17.3848 2.25 12ZM8.25 9.5625C8.25 8.83763 8.83763 8.25 9.5625 8.25H14.4375C15.1624 8.25 15.75 8.83763 15.75 9.5625V14.4375C15.75 15.1624 15.1624 15.75 14.4375 15.75H9.5625C8.83763 15.75 8.25 15.1624 8.25 14.4375V9.5625Z" fill="#ffffff"/>
</svg>
Stop Dictation
`;
// create a live transcript div
const liveTranscriptDiv = document.createElement('div');
liveTranscriptDiv.className = 'live-transcript';
// Create a container for the button and live transcript
let dictationContainer = document.createElement('div');
dictationContainer.className = 'dictation-container';
dictationContainer.appendChild(stopBtn);
dictationContainer.appendChild(liveTranscriptDiv);
const endDictation = () => {
shouldRestart = false;
if (dictationContainer.parentNode) {
dictationContainer.parentNode.removeChild(dictationContainer);
}
speechRecognition?.stop();
// Trigger context menu title change
chrome.runtime.sendMessage({
action: "updateContextMenu",
newTitle: "Start Dictation"
});
};
stopBtn.addEventListener('click', () => {
if (speechRecognition) {
endDictation();
}
});
chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
// Start dictation
if (msg.action === "startDictation") {
if (!('webkitSpeechRecognition' in window)) {
alert('Speech Recognition not supported');
return;
}
document.body.appendChild(dictationContainer);
speechRecognition = new webkitSpeechRecognition() || new SpeechRecognition();
speechRecognition.lang = navigator.language || 'en-US';
speechRecognition.interimResults = true;
speechRecognition.continuous = true;
//let lastInterimTranscript = '';
speechRecognition.onresult = function(event) {
let interimTranscript = '';
let finalTranscript = '';
for (let i = event.resultIndex; i < event.results.length; ++i) {
if (event.results[i].isFinal) {
finalTranscript += event.results[i][0].transcript;
} else {
interimTranscript += event.results[i][0].transcript;
}
}
/* const delta = interimTranscript.slice(lastInterimTranscript.length);
if (delta) {
simulateTyping(delta);
}
lastInterimTranscript = interimTranscript;*/
if(interimTranscript.length){
liveTranscriptDiv.style.display = 'block';
liveTranscriptDiv.textContent = interimTranscript;
lastResponseTime = Date.now();
}
if (finalTranscript) {
simulateTyping(finalTranscript);
liveTranscriptDiv.textContent = '';
liveTranscriptDiv.style.display = 'none';
}
}
speechRecognition.onend = () => {
const now = Date.now();
// If no response for 30 seconds or end triggered, assume user stopped speaking and end dictation
if (now - lastResponseTime > 30000 || !shouldRestart) {
endDictation();
return;
}
// Auto-restart to avoid unexpected stops
speechRecognition.start();
}
speechRecognition.onerror = (event) => {
console.error('Speech recognition error:', event.error);
// Optionally stop speechRecognition on critical errors
if (event.error === 'not-allowed' || event.error === 'service-not-allowed') {
endDictation();
}
};
speechRecognition.start();
// Trigger context menu title change
chrome.runtime.sendMessage({
action: "updateContextMenu",
newTitle: "Stop Dictation"
});
}
// Stop dictation
if (msg.action === "stopDictation") {
endDictation();
}
});
function simulateTyping(text) {
const activeElement = document.activeElement;
if (!activeElement) return;
let existingText = '';
if (activeElement.isContentEditable) {
existingText = activeElement.innerText || '';
} else if ('value' in activeElement) {
existingText = activeElement.value || '';
}
// Add space if last character is not space
const lastChar = existingText.slice(-1);
let textToType = text;
if (lastChar && lastChar !== ' ' && text[0] !== ' ') {
textToType = ' ' + text;
}
// Focused element should be an input, textarea, contenteditable or similar
if (activeElement.isContentEditable || activeElement.tagName === 'INPUT' || activeElement.tagName === 'TEXTAREA') {
// Insert each character as keyboard events
for (const char of textToType) {
const keyCode = char.charCodeAt(0);
const eventOptions = { key: char, char, keyCode, which: keyCode, bubbles: true };
activeElement.dispatchEvent(new KeyboardEvent('keydown', eventOptions));
activeElement.dispatchEvent(new KeyboardEvent('keypress', eventOptions));
if (activeElement.isContentEditable) {
// Insert text node for contenteditable
document.execCommand('insertText', false, char);
} else {
// Insert into input/textarea value and update cursor position
const start = activeElement.selectionStart;
const end = activeElement.selectionEnd;
const value = activeElement.value;
activeElement.value = value.slice(0, start) + char + value.slice(end);
activeElement.selectionStart = activeElement.selectionEnd = start + 1;
}
activeElement.dispatchEvent(new KeyboardEvent('keyup', eventOptions));
}
}
}