From 9006ffc3c19d0bf7081f3f4a574da4fe4fae0e08 Mon Sep 17 00:00:00 2001 From: Matt Flax Date: Sun, 11 May 2025 20:20:38 +1000 Subject: [PATCH] SpeechToText js : Simplify the populateChatInput function --- aider/gui_speech_to_text.js | 42 ++++++++++++------------------------- 1 file changed, 13 insertions(+), 29 deletions(-) diff --git a/aider/gui_speech_to_text.js b/aider/gui_speech_to_text.js index afe8c1854..8cd99d2d1 100644 --- a/aider/gui_speech_to_text.js +++ b/aider/gui_speech_to_text.js @@ -45,39 +45,23 @@ function populateChatInput(text) { const parentDoc = window.parent.document; let chatInput = parentDoc.querySelector('textarea[data-testid="stChatInputTextArea"]'); + const reactProps = Object.keys(chatInput).find(key => key.startsWith('__reactProps$')); + const syntheticEvent = { target: chatInput, currentTarget: chatInput, + preventDefault: () => {}, nativeEvent: new Event('input', { bubbles: true })}; - if (!chatInput) { - console.error("Could not find chat input textarea"); + if (!chatInput || !reactProps) { + if (!chatInput) + console.error("Could not find chat input textarea"); + if (!reactProps) + console.error("Error setting chat input value:", err); return false; } - try { - const reactProps = Object.keys(chatInput).find(key => key.startsWith('__reactProps$')); - - if (reactProps && chatInput[reactProps] && chatInput[reactProps].onChange) { - // Append to the existing value - chatInput.value = chatInput.value + ' ' + text; - - // Create a simplified synthetic event with only essential properties - const syntheticEvent = { - target: chatInput, - currentTarget: chatInput, - preventDefault: () => {}, - nativeEvent: new Event('input', { bubbles: true }) - }; - - // Call React's onChange handler - chatInput[reactProps].onChange(syntheticEvent); - } else { - console.error("Could not find React props on chat input"); - return false; - } - - return true; - } catch (err) { - console.error("Error setting chat input value:", err); - return false; - } + // Append to the existing value + chatInput.value = chatInput.value + ' ' + text; + // Call React's onChange handler + chatInput[reactProps].onChange(syntheticEvent); + return true; } // Initialize speech recognition