Fix input field focus with proper selectors for duck.ai

This commit is contained in:
Alex Abudaev 2026-04-15 16:45:05 +08:00
parent ffb1b8dc0d
commit 7349e4ef0c

View file

@ -87,18 +87,61 @@ class MainActivity : AppCompatActivity() {
webView.webViewClient = object : WebViewClient() { webView.webViewClient = object : WebViewClient() {
override fun onPageFinished(view: WebView?, url: String?) { override fun onPageFinished(view: WebView?, url: String?) {
view?.postDelayed({ view?.postDelayed({ tryFocusInput(view) }, 2000)
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)
} }
} }
} }
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() { private fun checkPermissionAndOpenPicker() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
openFilePicker() openFilePicker()
@ -154,9 +197,20 @@ class MainActivity : AppCompatActivity() {
webView.loadUrl(url) webView.loadUrl(url)
window.decorView.postDelayed({ 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() showKeyboard()
}, 800) }, 500)
} }
private fun showKeyboard() { private fun showKeyboard() {
@ -184,9 +238,8 @@ class MainActivity : AppCompatActivity() {
val url = "$BASE_URL/?q=${Uri.encode(query)}" val url = "$BASE_URL/?q=${Uri.encode(query)}"
webView.loadUrl(url) webView.loadUrl(url)
webView.postDelayed({ webView.postDelayed({
webView.requestFocus() requestFocusAndShowKeyboard()
showKeyboard() }, 1200)
}, 1000)
} }
} }