From 3e563f4a30ab3e258e54c1ceb9679e468da2a0ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=B8=BF=E8=92=99=E6=9C=BA=E5=99=A8=E4=BA=BA?= <113450723+Aziteee@users.noreply.github.com> Date: Wed, 20 Nov 2024 16:11:23 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=80=9A=E8=BF=87=20webview=20?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E4=B9=A6=E6=BA=90=E5=86=85=E5=AE=B9=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E8=AE=BE=E7=BD=AE=E5=BB=B6=E8=BF=9F=E6=97=B6=E9=97=B4?= =?UTF-8?q?=20(#4365)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../legado/app/help/http/BackstageWebView.kt | 29 ++++++++++++------- .../app/model/analyzeRule/AnalyzeUrl.kt | 22 ++++++++++++-- 2 files changed, 38 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/legado/app/help/http/BackstageWebView.kt b/app/src/main/java/io/legado/app/help/http/BackstageWebView.kt index 1581c7cf2438..f0243a3e4b35 100644 --- a/app/src/main/java/io/legado/app/help/http/BackstageWebView.kt +++ b/app/src/main/java/io/legado/app/help/http/BackstageWebView.kt @@ -5,6 +5,7 @@ import android.net.http.SslError import android.os.Handler import android.os.Looper import android.util.AndroidRuntimeException +import android.util.Log import android.webkit.CookieManager import android.webkit.SslErrorHandler import android.webkit.WebResourceRequest @@ -18,6 +19,7 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.utils.runOnUI import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Runnable +import kotlinx.coroutines.delay import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.withTimeout import org.apache.commons.text.StringEscapeUtils @@ -38,6 +40,7 @@ class BackstageWebView( private val sourceRegex: String? = null, private val overrideUrlRegex: String? = null, private val javaScript: String? = null, + private val delayTime: Long = 1000, ) { private val mHandler = Handler(Looper.getMainLooper()) @@ -53,8 +56,9 @@ class BackstageWebView( } callback = object : Callback() { override fun onResult(response: StrResponse) { - if (!block.isCompleted) + if (!block.isCompleted) { block.resume(response) + } } override fun onError(error: Throwable) { @@ -145,11 +149,13 @@ class BackstageWebView( override fun onPageFinished(view: WebView, url: String) { setCookie(url) - if (runnable == null) { - runnable = EvalJsRunnable(view, url, getJs()) - } - mHandler.removeCallbacks(runnable!!) - mHandler.postDelayed(runnable!!, 1000) + mHandler.postDelayed({ + if (runnable == null) { + runnable = EvalJsRunnable(view, url, getJs()) + } + mHandler.removeCallbacks(runnable!!) + mHandler.postDelayed(runnable!!, 1000) + }, delayTime) } @SuppressLint("WebViewClientOnReceivedSslError") @@ -176,6 +182,7 @@ class BackstageWebView( private fun handleResult(result: String) = Coroutine.async { if (result.isNotEmpty() && result != "null") { + Log.d("BackstageWebView", "result: $result") val content = StringEscapeUtils.unescapeJson(result) .replace(quoteRegex, "") try { @@ -255,10 +262,12 @@ class BackstageWebView( override fun onPageFinished(webView: WebView, url: String) { setCookie(url) - if (!javaScript.isNullOrEmpty()) { - val runnable = LoadJsRunnable(webView, javaScript) - mHandler.postDelayed(runnable, 1000L) - } + mHandler.postDelayed({ + if (!javaScript.isNullOrEmpty()) { + val runnable = LoadJsRunnable(webView, javaScript) + mHandler.postDelayed(runnable, 1000L) + } + }, delayTime) } @SuppressLint("WebViewClientOnReceivedSslError") diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 49b95ab10720..a1930392b6e9 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -87,6 +87,7 @@ class AnalyzeUrl( private var webJs: String? = null private val enabledCookieJar = source?.enabledCookieJar ?: false private val domain: String + private var delayTime: Long? = null // 服务器ID var serverID: Long? = null @@ -215,6 +216,7 @@ class AnalyzeUrl( } } serverID = option.getServerID() + delayTime = option.getDelayTime() } } urlNoQuery = url @@ -433,7 +435,8 @@ class AnalyzeUrl( tag = source?.getKey(), javaScript = webJs ?: jsStr, sourceRegex = sourceRegex, - headerMap = headerMap + headerMap = headerMap, + delayTime = delayTime ?:1000 ).getStrResponse() } @@ -442,7 +445,8 @@ class AnalyzeUrl( tag = source?.getKey(), javaScript = webJs ?: jsStr, sourceRegex = sourceRegex, - headerMap = headerMap + headerMap = headerMap, + delayTime = delayTime ?:1000 ).getStrResponse() } } else { @@ -704,7 +708,11 @@ class AnalyzeUrl( /** * 服务器id */ - private var serverID: Long? = null + private var serverID: Long? = null, + /** + * webview等待页面加载完毕的延迟时间(毫秒) + */ + private var delayTime: Long? = null, ) { fun setMethod(value: String?) { method = if (value.isNullOrBlank()) null else value @@ -811,6 +819,14 @@ class AnalyzeUrl( fun getServerID(): Long? { return serverID } + + fun setDelayTime(value: String?) { + delayTime = if (value.isNullOrBlank()) null else value.toLong() + } + + fun getDelayTime(): Long? { + return delayTime + } } data class ConcurrentRecord(