feat: 通过 webview 获取书源内容支持设置延迟时间 (#4365)

This commit is contained in:
鸿蒙机器人
2024-11-20 16:11:23 +08:00
committed by GitHub
parent a04ca8717b
commit d384254eb9
2 changed files with 38 additions and 13 deletions
@@ -5,6 +5,7 @@ import android.net.http.SslError
import android.os.Handler import android.os.Handler
import android.os.Looper import android.os.Looper
import android.util.AndroidRuntimeException import android.util.AndroidRuntimeException
import android.util.Log
import android.webkit.CookieManager import android.webkit.CookieManager
import android.webkit.SslErrorHandler import android.webkit.SslErrorHandler
import android.webkit.WebResourceRequest import android.webkit.WebResourceRequest
@@ -18,6 +19,7 @@ import io.legado.app.help.coroutine.Coroutine
import io.legado.app.utils.runOnUI import io.legado.app.utils.runOnUI
import kotlinx.coroutines.Dispatchers.IO import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.Runnable import kotlinx.coroutines.Runnable
import kotlinx.coroutines.delay
import kotlinx.coroutines.suspendCancellableCoroutine import kotlinx.coroutines.suspendCancellableCoroutine
import kotlinx.coroutines.withTimeout import kotlinx.coroutines.withTimeout
import org.apache.commons.text.StringEscapeUtils import org.apache.commons.text.StringEscapeUtils
@@ -38,6 +40,7 @@ class BackstageWebView(
private val sourceRegex: String? = null, private val sourceRegex: String? = null,
private val overrideUrlRegex: String? = null, private val overrideUrlRegex: String? = null,
private val javaScript: String? = null, private val javaScript: String? = null,
private val delayTime: Long = 1000,
) { ) {
private val mHandler = Handler(Looper.getMainLooper()) private val mHandler = Handler(Looper.getMainLooper())
@@ -53,8 +56,9 @@ class BackstageWebView(
} }
callback = object : Callback() { callback = object : Callback() {
override fun onResult(response: StrResponse) { override fun onResult(response: StrResponse) {
if (!block.isCompleted) if (!block.isCompleted) {
block.resume(response) block.resume(response)
}
} }
override fun onError(error: Throwable) { override fun onError(error: Throwable) {
@@ -145,11 +149,13 @@ class BackstageWebView(
override fun onPageFinished(view: WebView, url: String) { override fun onPageFinished(view: WebView, url: String) {
setCookie(url) setCookie(url)
if (runnable == null) { mHandler.postDelayed({
runnable = EvalJsRunnable(view, url, getJs()) if (runnable == null) {
} runnable = EvalJsRunnable(view, url, getJs())
mHandler.removeCallbacks(runnable!!) }
mHandler.postDelayed(runnable!!, 1000) mHandler.removeCallbacks(runnable!!)
mHandler.postDelayed(runnable!!, 1000)
}, delayTime)
} }
@SuppressLint("WebViewClientOnReceivedSslError") @SuppressLint("WebViewClientOnReceivedSslError")
@@ -176,6 +182,7 @@ class BackstageWebView(
private fun handleResult(result: String) = Coroutine.async { private fun handleResult(result: String) = Coroutine.async {
if (result.isNotEmpty() && result != "null") { if (result.isNotEmpty() && result != "null") {
Log.d("BackstageWebView", "result: $result")
val content = StringEscapeUtils.unescapeJson(result) val content = StringEscapeUtils.unescapeJson(result)
.replace(quoteRegex, "") .replace(quoteRegex, "")
try { try {
@@ -255,10 +262,12 @@ class BackstageWebView(
override fun onPageFinished(webView: WebView, url: String) { override fun onPageFinished(webView: WebView, url: String) {
setCookie(url) setCookie(url)
if (!javaScript.isNullOrEmpty()) { mHandler.postDelayed({
val runnable = LoadJsRunnable(webView, javaScript) if (!javaScript.isNullOrEmpty()) {
mHandler.postDelayed(runnable, 1000L) val runnable = LoadJsRunnable(webView, javaScript)
} mHandler.postDelayed(runnable, 1000L)
}
}, delayTime)
} }
@SuppressLint("WebViewClientOnReceivedSslError") @SuppressLint("WebViewClientOnReceivedSslError")
@@ -87,6 +87,7 @@ class AnalyzeUrl(
private var webJs: String? = null private var webJs: String? = null
private val enabledCookieJar = source?.enabledCookieJar ?: false private val enabledCookieJar = source?.enabledCookieJar ?: false
private val domain: String private val domain: String
private var delayTime: Long? = null
// 服务器ID // 服务器ID
var serverID: Long? = null var serverID: Long? = null
@@ -215,6 +216,7 @@ class AnalyzeUrl(
} }
} }
serverID = option.getServerID() serverID = option.getServerID()
delayTime = option.getDelayTime()
} }
} }
urlNoQuery = url urlNoQuery = url
@@ -433,7 +435,8 @@ class AnalyzeUrl(
tag = source?.getKey(), tag = source?.getKey(),
javaScript = webJs ?: jsStr, javaScript = webJs ?: jsStr,
sourceRegex = sourceRegex, sourceRegex = sourceRegex,
headerMap = headerMap headerMap = headerMap,
delayTime = delayTime ?:1000
).getStrResponse() ).getStrResponse()
} }
@@ -442,7 +445,8 @@ class AnalyzeUrl(
tag = source?.getKey(), tag = source?.getKey(),
javaScript = webJs ?: jsStr, javaScript = webJs ?: jsStr,
sourceRegex = sourceRegex, sourceRegex = sourceRegex,
headerMap = headerMap headerMap = headerMap,
delayTime = delayTime ?:1000
).getStrResponse() ).getStrResponse()
} }
} else { } else {
@@ -704,7 +708,11 @@ class AnalyzeUrl(
/** /**
* 服务器id * 服务器id
*/ */
private var serverID: Long? = null private var serverID: Long? = null,
/**
* webview等待页面加载完毕的延迟时间(毫秒)
*/
private var delayTime: Long? = null,
) { ) {
fun setMethod(value: String?) { fun setMethod(value: String?) {
method = if (value.isNullOrBlank()) null else value method = if (value.isNullOrBlank()) null else value
@@ -811,6 +819,14 @@ class AnalyzeUrl(
fun getServerID(): Long? { fun getServerID(): Long? {
return serverID return serverID
} }
fun setDelayTime(value: String?) {
delayTime = if (value.isNullOrBlank()) null else value.toLong()
}
fun getDelayTime(): Long? {
return delayTime
}
} }
data class ConcurrentRecord( data class ConcurrentRecord(