From 7349e4ef0c3dc73535c25842849d2dea85724102 Mon Sep 17 00:00:00 2001 From: Alex Abudaev Date: Wed, 15 Apr 2026 16:45:05 +0800 Subject: [PATCH] Fix input field focus with proper selectors for duck.ai --- .../java/com/duckai/app/web/MainActivity.kt | 81 +++++++++++++++---- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/app/src/main/java/com/duckai/app/web/MainActivity.kt b/app/src/main/java/com/duckai/app/web/MainActivity.kt index bf3a348..4373761 100644 --- a/app/src/main/java/com/duckai/app/web/MainActivity.kt +++ b/app/src/main/java/com/duckai/app/web/MainActivity.kt @@ -87,18 +87,61 @@ class MainActivity : AppCompatActivity() { webView.webViewClient = object : WebViewClient() { override fun onPageFinished(view: WebView?, url: String?) { - view?.postDelayed({ - view.evaluateJavascript( - "setTimeout(() => {" + - " const input = document.querySelector('input[type=\"text\"], textarea[id*=\"message\"], [role=\"combobox\"], input[type=\"file\"]');" + - " if(input) { input.focus(); input.click(); }" + - "}, 100);" - ) { _ -> } - }, 800) + view?.postDelayed({ tryFocusInput(view) }, 2000) } } } - + + private fun tryFocusInput(view: WebView?) { + view?.evaluateJavascript( + "(function() {" + + " const selectors = [" + + " 'textarea[name=\"user-prompt\"]'," + + " 'textarea[placeholder=\"Ask privately\"]'," + + " 'textarea.JRDRiEf5NPKWK43sArdC'," + + " 'textarea[id*=\"message\"]'," + + " 'textarea[name*=\"message\"]'," + + " 'textarea[placeholder*=\"Сообщ\"]'," + + " 'textarea[placeholder*=\"Message\"]'," + + " 'textarea[data-id]'," + + " 'div[contenteditable=\"true\"]'," + + " 'div[role=\"textbox\"]'" + + " ];" + + " " + + " for (const sel of selectors) {" + + " const el = document.querySelector(sel);" + + " if (el && el.offsetParent !== null) {" + + " el.style.visibility = 'visible';" + + " el.style.display = 'block';" + + " el.scrollIntoView({block: 'center', behavior: 'instant'});" + + " el.focus();" + + " if (el.tagName === 'TEXTAREA' && el.setSelectionRange) {" + + " el.setSelectionRange(el.value?.length || 0, el.value?.length || 0);" + + " }" + + " return 'FOUND: ' + sel;" + + " }" + + " }" + + " " + + " const allTextareas = document.querySelectorAll('textarea');" + + " for (const el of allTextareas) {" + + " if (el.offsetParent !== null && el.clientHeight > 20) {" + + " el.style.visibility = 'visible';" + + " el.style.display = 'block';" + + " el.scrollIntoView({block: 'center', behavior: 'instant'});" + + " el.focus();" + + " return 'FOUND fallback: ' + el.name + ' class=' + el.className;" + + " }" + + " }" + + " " + + " return 'NOT FOUND';" + + "})();" + ) { result -> + if (result?.contains("NOT FOUND") == true) { + android.util.Log.d("DuckAI", "Focus result: $result") + } + } + } + private fun checkPermissionAndOpenPicker() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { openFilePicker() @@ -154,9 +197,20 @@ class MainActivity : AppCompatActivity() { webView.loadUrl(url) window.decorView.postDelayed({ - webView.requestFocus() + requestFocusAndShowKeyboard() + }, 1200) + } + + private fun requestFocusAndShowKeyboard() { + webView.requestFocus() + + // Try to focus input field + tryFocusInput(webView) + + // Show keyboard after JS + window.decorView.postDelayed({ showKeyboard() - }, 800) + }, 500) } private fun showKeyboard() { @@ -184,9 +238,8 @@ class MainActivity : AppCompatActivity() { val url = "$BASE_URL/?q=${Uri.encode(query)}" webView.loadUrl(url) webView.postDelayed({ - webView.requestFocus() - showKeyboard() - }, 1000) + requestFocusAndShowKeyboard() + }, 1200) } }