Skip to content

Commit

Permalink
feat: 通过 webview 获取书源内容支持设置延迟时间 (gedoor#4365)
Browse files Browse the repository at this point in the history
  • Loading branch information
Aziteee authored Nov 20, 2024
1 parent d0f4399 commit 3e563f4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 13 deletions.
29 changes: 19 additions & 10 deletions app/src/main/java/io/legado/app/help/http/BackstageWebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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())
Expand All @@ -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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
22 changes: 19 additions & 3 deletions app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -215,6 +216,7 @@ class AnalyzeUrl(
}
}
serverID = option.getServerID()
delayTime = option.getDelayTime()
}
}
urlNoQuery = url
Expand Down Expand Up @@ -433,7 +435,8 @@ class AnalyzeUrl(
tag = source?.getKey(),
javaScript = webJs ?: jsStr,
sourceRegex = sourceRegex,
headerMap = headerMap
headerMap = headerMap,
delayTime = delayTime ?:1000
).getStrResponse()
}

Expand All @@ -442,7 +445,8 @@ class AnalyzeUrl(
tag = source?.getKey(),
javaScript = webJs ?: jsStr,
sourceRegex = sourceRegex,
headerMap = headerMap
headerMap = headerMap,
delayTime = delayTime ?:1000
).getStrResponse()
}
} else {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 3e563f4

Please sign in to comment.