feat: per-app UA config, fix demo file path and back-loop

This commit is contained in:
2026-08-17 19:51:54 +08:00
parent 5498c8ed0b
commit 24c78bb9c2
6 changed files with 64 additions and 17 deletions
@@ -267,13 +267,16 @@ class MainActivity : Activity() {
private val sidebarWidthPx by lazy {
(SIDEBAR_WIDTH_DP * resources.displayMetrics.density).toInt()
}
// 加载失败的 webapp idBack 键时直接关闭而非 goBack 循环回退
// Failed webapp ids: on Back, close directly instead of looping goBack
private val failedWebViews = mutableSetOf<String>()
override fun openWebView(id: String, url: String) {
override fun openWebView(id: String, url: String, ua: String?) {
if (contentWebViews.containsKey(id)) {
switchWebView(id)
return
}
val webView = WebViewManager(this@MainActivity).createContentWebView()
val webView = WebViewManager(this@MainActivity).createContentWebView(ua)
webView.webViewClient = object : WebViewClient() {
@Suppress("DEPRECATION", "OVERRIDE_DEPRECATION")
override fun onReceivedError(
@@ -283,6 +286,7 @@ class MainActivity : Activity() {
failingUrl: String?,
) {
Log.d(TAG, "content onReceivedError: code=$code desc=$description url=$failingUrl")
failedWebViews.add(id)
view?.loadDataWithBaseURL(null, errorPage(description ?: "未知错误"), "text/html", "utf-8", null)
}
}
@@ -311,6 +315,7 @@ class MainActivity : Activity() {
override fun closeWebView(id: String) {
val webView = contentWebViews.remove(id) ?: return
failedWebViews.remove(id)
root.removeView(webView)
webView.stopLoading()
webView.destroy()
@@ -333,8 +338,12 @@ class MainActivity : Activity() {
}
override fun goBack(): Boolean {
val webView = currentVisibleId?.let { contentWebViews[it] }
return if (webView != null && webView.canGoBack()) {
val id = currentVisibleId ?: return false
// 加载失败的 webappBack 直接关闭(返回 false 让 onBackPressed 关闭标签),
// 避免 goBack 回退到失败的 file:// 页面反复加载
if (failedWebViews.contains(id)) return false
val webView = contentWebViews[id] ?: return false
return if (webView.canGoBack()) {
webView.goBack()
true
} else {