@@ -3,6 +3,8 @@ let isTranscribing = false;
3
3
let audioContext ;
4
4
let mediaRecorder ;
5
5
let audioChunks = [ ] ;
6
+ let clickedMicId = "" ;
7
+
6
8
const MAX_RETRIES = 3 ;
7
9
const RETRY_DELAY = 100 ; // 1 second
8
10
const WHISPER_SAMPLING_RATE = 16000 ;
@@ -79,10 +81,11 @@ function insertMicrophoneButton(targetDiv, inline) {
79
81
}
80
82
}
81
83
82
- function checkSettingsAndToggleRecording ( ) {
83
- chrome . storage . sync . get ( [ 'model' , 'apiKey' ] , function ( result ) {
84
- if ( result . model === 'groq' && ! result . apiKey ) {
85
- showError ( "Groq API key not set. Please set it in the extension options." ) ;
84
+ function checkSettingsAndToggleRecording ( evt ) {
85
+ const button = evt . currentTarget ;
86
+ chrome . storage . sync . get ( [ 'model' , 'groqApiKey' , 'openaiApiKey' ] , function ( result ) {
87
+ if ( ( result . model === 'groq' && ! result . groqApiKey ) || ( result . model === 'openai' && ! result . openaiApiKey ) ) {
88
+ showError ( "API key not set. Please set it in the extension options from the Chrome extension menu." ) ;
86
89
return ;
87
90
}
88
91
@@ -91,13 +94,17 @@ function checkSettingsAndToggleRecording() {
91
94
if ( localResult . modelLoadError ) {
92
95
showError ( "WebGPU model failed to load. Please try again or switch to Groq in the extension options." ) ;
93
96
} else {
94
- toggleRecording ( ) ;
97
+ toggleRecording ( evt , button ) ;
95
98
}
96
99
} ) ;
97
100
} else {
98
- toggleRecording ( ) ;
101
+ toggleRecording ( evt , button ) ;
99
102
}
100
103
} ) ;
104
+
105
+ evt . preventDefault ( ) ;
106
+ evt . stopPropagation ( ) ;
107
+ return false ;
101
108
}
102
109
103
110
function showError ( message ) {
@@ -115,7 +122,7 @@ function showError(message) {
115
122
closeButton . onclick = closeError ;
116
123
117
124
// Disable the mic button
118
- const micButton = document . getElementById ( "mic-button" ) ;
125
+ const micButton = document . getElementById ( clickedMicId ) ;
119
126
if ( micButton ) {
120
127
micButton . disabled = true ;
121
128
micButton . style . opacity = "0.5" ;
@@ -136,15 +143,15 @@ function closeError() {
136
143
infoSpeechDiv . textContent = "" ;
137
144
}
138
145
// Re-enable the mic button
139
- const micButton = document . getElementById ( "mic-button" ) ;
146
+ const micButton = document . getElementById ( clickedMicId ) ;
140
147
if ( micButton ) {
141
148
micButton . disabled = false ;
142
149
micButton . style . opacity = "1" ;
143
150
}
144
151
}
145
152
146
- function toggleRecording ( obj ) {
147
- clickedMicId = obj . currentTarget //.id;
153
+ function toggleRecording ( evt , button ) {
154
+ clickedMicId = button ; // obj.currentTarget //.id;
148
155
closeError ( ) ; // Close any existing error message
149
156
const micButton = clickedMicId // document.getElementById(clickedMicId);
150
157
if ( ! isRecording && ! isTranscribing ) {
@@ -160,9 +167,9 @@ function toggleRecording(obj) {
160
167
}
161
168
162
169
// cancel the event
163
- obj . preventDefault ( ) ;
170
+ evt . preventDefault ( ) ;
164
171
// avoid the event to propagate
165
- obj . stopPropagation ( ) ;
172
+ evt . stopPropagation ( ) ;
166
173
167
174
return false ;
168
175
}
@@ -212,6 +219,24 @@ function stopRecording() {
212
219
}
213
220
214
221
222
+ // audioBlob
223
+ // .arrayBuffer()
224
+ // .then((buffer) => {
225
+ // // Send the audio buffer to the background script for transcription
226
+ // chrome.runtime.sendMessage(
227
+ // {
228
+ // action: "transcribe",
229
+ // audioBuffer: Array.from(new Uint8Array(buffer)), // Convert ArrayBuffer to array
230
+ // micId: clickedMicId.id,
231
+ // },
232
+ // handleTranscription
233
+ // );
234
+ // })
235
+ // .catch((error) => {
236
+ // console.error("Error converting blob to array buffer:", error);
237
+ // showError("Error processing audio. Please try again.");
238
+ // resetRecordingState();
239
+ // });
215
240
216
241
async function transcribeAudio ( retryCount = 0 ) {
217
242
if ( retryCount >= MAX_RETRIES ) {
@@ -250,6 +275,7 @@ async function transcribeAudio(retryCount = 0) {
250
275
{
251
276
action : "transcribe" ,
252
277
audioBuffer : Array . from ( uint8Array ) ,
278
+ micId : clickedMicId . id ,
253
279
} ,
254
280
( response ) => {
255
281
if ( chrome . runtime . lastError ) {
@@ -276,16 +302,16 @@ function showRetryMessage(retryCount) {
276
302
}
277
303
278
304
function handleTranscription ( response ) {
279
- const micId = response . micId ;
305
+ clickedMicId = response . micId ;
280
306
281
- const micButton = document . getElementById ( micId ) ;
307
+ const micButton = document . getElementById ( clickedMicId ) ;
282
308
isTranscribing = false ;
283
309
micButton . disabled = false ;
284
310
micButton . style . animation = "" ;
285
311
micButton . innerHTML = recordIcon ;
286
312
287
313
if ( response . success && response . text ) {
288
- insertTranscribedText ( response . text , micId ) ;
314
+ insertTranscribedText ( response . text , clickedMicId ) ;
289
315
initializeExtension ( ) ;
290
316
} else {
291
317
showError ( response . error || "Transcription failed. Please try again." ) ;
@@ -393,11 +419,11 @@ document.head.appendChild(style);
393
419
// initializeExtension();
394
420
// }
395
421
396
- // function initializeExtension() {
397
- // insertMicrophoneButton();
398
- // console.log("Microphone button inserted");
399
- // // Add any other initialization code here
400
- // }
422
+ function initializeExtension ( ) {
423
+ // insertMicrophoneButton();
424
+ // console.log("Microphone button inserted");
425
+ // // Add any other initialization code here
426
+ }
401
427
402
428
// Every 100 ms check if mic button is present if not insert it
403
429
setInterval ( ( ) => {
0 commit comments