Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Nov 20, 2024
1 parent 3e563f4 commit 6cf5036
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 28 deletions.
27 changes: 10 additions & 17 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,7 +5,6 @@ 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 @@ -19,7 +18,6 @@ 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 @@ -40,7 +38,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 delayTime: Long = 0,
) {

private val mHandler = Handler(Looper.getMainLooper())
Expand Down Expand Up @@ -149,13 +147,11 @@ class BackstageWebView(

override fun onPageFinished(view: WebView, url: String) {
setCookie(url)
mHandler.postDelayed({
if (runnable == null) {
runnable = EvalJsRunnable(view, url, getJs())
}
mHandler.removeCallbacks(runnable!!)
mHandler.postDelayed(runnable!!, 1000)
}, delayTime)
if (runnable == null) {
runnable = EvalJsRunnable(view, url, getJs())
}
mHandler.removeCallbacks(runnable!!)
mHandler.postDelayed(runnable!!, 1000 + delayTime)
}

@SuppressLint("WebViewClientOnReceivedSslError")
Expand All @@ -182,7 +178,6 @@ 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 @@ -262,12 +257,10 @@ class BackstageWebView(

override fun onPageFinished(webView: WebView, url: String) {
setCookie(url)
mHandler.postDelayed({
if (!javaScript.isNullOrEmpty()) {
val runnable = LoadJsRunnable(webView, javaScript)
mHandler.postDelayed(runnable, 1000L)
}
}, delayTime)
if (!javaScript.isNullOrEmpty()) {
val runnable = LoadJsRunnable(webView, javaScript)
mHandler.postDelayed(runnable, 1000L + delayTime)
}
}

@SuppressLint("WebViewClientOnReceivedSslError")
Expand Down
44 changes: 33 additions & 11 deletions app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,31 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.config.AppConfig
import io.legado.app.help.exoplayer.ExoPlayerHelper
import io.legado.app.help.glide.GlideHeaders
import io.legado.app.help.http.*
import io.legado.app.help.http.BackstageWebView
import io.legado.app.help.http.CookieManager
import io.legado.app.help.http.CookieManager.mergeCookies
import io.legado.app.utils.*
import io.legado.app.help.http.CookieStore
import io.legado.app.help.http.RequestMethod
import io.legado.app.help.http.StrResponse
import io.legado.app.help.http.addHeaders
import io.legado.app.help.http.get
import io.legado.app.help.http.getProxyClient
import io.legado.app.help.http.newCallResponse
import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.postForm
import io.legado.app.help.http.postJson
import io.legado.app.help.http.postMultipart
import io.legado.app.utils.EncoderUtils
import io.legado.app.utils.GSON
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.get
import io.legado.app.utils.isJson
import io.legado.app.utils.isJsonArray
import io.legado.app.utils.isJsonObject
import io.legado.app.utils.isXml
import io.legado.app.utils.splitNotBlank
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
import okhttp3.MediaType.Companion.toMediaType
Expand Down Expand Up @@ -87,7 +109,7 @@ class AnalyzeUrl(
private var webJs: String? = null
private val enabledCookieJar = source?.enabledCookieJar ?: false
private val domain: String
private var delayTime: Long? = null
private var webViewDelayTime: Long = 0

// 服务器ID
var serverID: Long? = null
Expand Down Expand Up @@ -216,7 +238,7 @@ class AnalyzeUrl(
}
}
serverID = option.getServerID()
delayTime = option.getDelayTime()
webViewDelayTime = max(0, option.getWebViewDelayTime() ?: 0)
}
}
urlNoQuery = url
Expand Down Expand Up @@ -436,7 +458,7 @@ class AnalyzeUrl(
javaScript = webJs ?: jsStr,
sourceRegex = sourceRegex,
headerMap = headerMap,
delayTime = delayTime ?:1000
delayTime = webViewDelayTime
).getStrResponse()
}

Expand All @@ -446,7 +468,7 @@ class AnalyzeUrl(
javaScript = webJs ?: jsStr,
sourceRegex = sourceRegex,
headerMap = headerMap,
delayTime = delayTime ?:1000
delayTime = webViewDelayTime
).getStrResponse()
}
} else {
Expand Down Expand Up @@ -712,7 +734,7 @@ class AnalyzeUrl(
/**
* webview等待页面加载完毕的延迟时间(毫秒)
*/
private var delayTime: Long? = null,
private var webViewDelayTime: Long? = null,
) {
fun setMethod(value: String?) {
method = if (value.isNullOrBlank()) null else value
Expand Down Expand Up @@ -820,12 +842,12 @@ class AnalyzeUrl(
return serverID
}

fun setDelayTime(value: String?) {
delayTime = if (value.isNullOrBlank()) null else value.toLong()
fun setWebViewDelayTime(value: String?) {
webViewDelayTime = if (value.isNullOrBlank()) null else value.toLong()
}

fun getDelayTime(): Long? {
return delayTime
fun getWebViewDelayTime(): Long? {
return webViewDelayTime
}
}

Expand Down

0 comments on commit 6cf5036

Please sign in to comment.