@@ -229,31 +229,59 @@ function setupVersionPrefill(): void {
229229 window . Kapa ( 'onModalOpen' , ( ) => {
230230 console . log ( '[AskAI] Modal opened' ) ;
231231
232- // Wait a moment for the input to be rendered
233- setTimeout ( ( ) => {
234- // Find the textarea input
235- const textarea = document . querySelector < HTMLTextAreaElement > (
236- 'textarea[placeholder*="Ask"]'
237- ) ;
238- console . log ( '[AskAI] Textarea found:' , ! ! textarea ) ;
239-
240- if ( textarea && ( ! textarea . value || textarea . value . trim ( ) === '' ) ) {
241- console . log ( '[AskAI] Setting textarea value to:' , versionContext ) ;
242- textarea . value = versionContext ;
243-
244- // Dispatch input event to notify React
245- const inputEvent = new Event ( 'input' , { bubbles : true } ) ;
246- textarea . dispatchEvent ( inputEvent ) ;
247-
248- // Focus at the beginning so user can start typing
249- textarea . setSelectionRange ( 0 , 0 ) ;
250- textarea . focus ( ) ;
251-
252- console . log ( '[AskAI] Version context added to input' ) ;
232+ // Try multiple times with different delays to find the textarea
233+ const trySetValue = ( attempt = 0 ) : void => {
234+ console . log ( `[AskAI] Attempt ${ attempt + 1 } to find textarea` ) ;
235+
236+ // Try multiple selectors
237+ const selectors = [
238+ 'textarea[placeholder*="Ask"]' ,
239+ 'textarea[placeholder*="ask"]' ,
240+ 'textarea' ,
241+ '#kapa-widget-container textarea' ,
242+ '[data-kapa-widget] textarea' ,
243+ ] ;
244+
245+ let textarea : HTMLTextAreaElement | null = null ;
246+ for ( const selector of selectors ) {
247+ textarea = document . querySelector < HTMLTextAreaElement > ( selector ) ;
248+ if ( textarea ) {
249+ console . log ( `[AskAI] Found textarea with selector: ${ selector } ` ) ;
250+ break ;
251+ }
252+ }
253+
254+ if ( textarea ) {
255+ // Check if it already has a value
256+ if ( ! textarea . value || textarea . value . trim ( ) === '' ) {
257+ console . log ( '[AskAI] Setting textarea value to:' , versionContext ) ;
258+ textarea . value = versionContext ;
259+
260+ // Dispatch multiple events to ensure React picks it up
261+ const inputEvent = new Event ( 'input' , { bubbles : true } ) ;
262+ const changeEvent = new Event ( 'change' , { bubbles : true } ) ;
263+ textarea . dispatchEvent ( inputEvent ) ;
264+ textarea . dispatchEvent ( changeEvent ) ;
265+
266+ // Focus at the beginning so user can start typing
267+ textarea . setSelectionRange ( 0 , 0 ) ;
268+ textarea . focus ( ) ;
269+
270+ console . log ( '[AskAI] Version context added to input' ) ;
271+ } else {
272+ console . log ( '[AskAI] Textarea already has value:' , textarea . value ) ;
273+ }
274+ } else if ( attempt < 5 ) {
275+ // Try again with increasing delays
276+ const delay = ( attempt + 1 ) * 100 ;
277+ console . log ( `[AskAI] Textarea not found, retrying in ${ delay } ms` ) ;
278+ setTimeout ( ( ) => trySetValue ( attempt + 1 ) , delay ) ;
253279 } else {
254- console . log ( '[AskAI] Textarea already has value or not found ' ) ;
280+ console . log ( '[AskAI] Failed to find textarea after 5 attempts ' ) ;
255281 }
256- } , 100 ) ;
282+ } ;
283+
284+ trySetValue ( ) ;
257285 } ) ;
258286
259287 console . log ( '[AskAI] Version pre-fill setup complete' ) ;
0 commit comments