@@ -23,14 +23,6 @@ async function processAIStudioCustomSendButtonClick(event, customText, autoSend)
2323 logConCgp ( '[AIStudio] Found editor using SelectorGuard' ) ;
2424 }
2525
26- // Use SelectorGuard to find the send button (enables heuristics)
27- // Note: We find it early to check existence, but we'll find it again during auto-send
28- // to ensure we have the latest reference if the DOM updated.
29- const sendButton = await window . OneClickPromptsSelectorGuard . findSendButton ( ) ;
30- if ( sendButton ) {
31- logConCgp ( '[buttons] AI Studio Send button found using SelectorGuard' ) ;
32- }
33-
3426 if ( ! editorArea ) {
3527 logConCgp ( '[buttons] AI Studio Editor not found. Unable to proceed.' ) ;
3628 showToast ( 'Could not find the text input area.' , 'error' ) ;
@@ -53,17 +45,45 @@ async function processAIStudioCustomSendButtonClick(event, customText, autoSend)
5345 // Auto-send if enabled
5446 if ( globalMaxExtensionConfig . globalAutoSendEnabled && autoSend ) {
5547 logConCgp ( '[buttons] AI Studio Auto-send enabled, attempting to send message' ) ;
56- // Use setTimeout to ensure text is processed before sending
57- // Use setTimeout to ensure text is processed before sending
58- setTimeout ( async ( ) => {
59- // Re-fetch send button to be safe (and use heuristics if needed)
60- const btn = await window . OneClickPromptsSelectorGuard . findSendButton ( ) ;
48+
49+ // Robust retry mechanism: poll for send button with multiple attempts
50+ const MAX_ATTEMPTS = 10 ; // 2 seconds max (10 × 200ms)
51+ let attempts = 0 ;
52+ let pollInterval ;
53+
54+ const attemptSend = async ( ) => {
55+ attempts ++ ;
56+ logConCgp ( `[buttons] AI Studio auto-send attempt ${ attempts } /${ MAX_ATTEMPTS } ` ) ;
57+
58+ let btn = null ;
59+
60+ // For all attempts except the last one, use silent selector queries
61+ // This avoids triggering auto-detector during normal DOM update delays
62+ if ( attempts < MAX_ATTEMPTS ) {
63+ // Silent query without triggering auto-detector
64+ const selectors = window . InjectionTargetsOnWebsite ?. selectors ?. sendButtons || [ ] ;
65+ btn = window . OneClickPromptsSelectorGuard . _querySelectors ( selectors , { requireEnabled : true } ) ;
66+ } else {
67+ // On final attempt, use full SelectorGuard which enables auto-detector and heuristics
68+ btn = await window . OneClickPromptsSelectorGuard . findSendButton ( ) ;
69+ }
70+
6171 if ( btn ) {
72+ logConCgp ( '[buttons] AI Studio Send button found, clicking now' ) ;
6273 MaxExtensionUtils . simulateClick ( btn ) ;
63- } else {
64- logConCgp ( '[buttons] AI Studio Send button not found for auto-send.' ) ;
65- showToast ( 'Could not find the send button.' , 'error' ) ;
74+ clearInterval ( pollInterval ) ;
75+ } else if ( attempts >= MAX_ATTEMPTS ) {
76+ // Error already shown by SelectorGuard on final attempt
77+ logConCgp ( '[buttons] AI Studio Send button not found after all retry attempts' ) ;
78+ clearInterval ( pollInterval ) ;
6679 }
80+ // If not found and attempts remain, keep polling silently
81+ } ;
82+
83+ // Start polling after initial delay to let text settle
84+ setTimeout ( ( ) => {
85+ pollInterval = setInterval ( attemptSend , 200 ) ;
86+ attemptSend ( ) ; // Immediate first attempt
6787 } , 100 ) ;
6888 }
6989}
0 commit comments