Fix input field focus with proper selectors for duck.ai
This commit is contained in:
parent
ffb1b8dc0d
commit
7349e4ef0c
1 changed files with 67 additions and 14 deletions
|
|
@ -87,14 +87,57 @@ 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")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue