startBrowserAwait 添加 refetchAfterSuccess 参数,为 true 时返回的页面源码为用户看见的页面内容

This commit is contained in:
Nihility
2024-09-09 15:02:08 +08:00
parent 8bfa1d3129
commit f20ad82ae4
5 changed files with 35 additions and 14 deletions
@@ -226,10 +226,10 @@ interface JsExtensions : JsEncodeUtils {
/** /**
* 使用内置浏览器打开链接,并等待网页结果 * 使用内置浏览器打开链接,并等待网页结果
*/ */
fun startBrowserAwait(url: String, title: String): StrResponse { fun startBrowserAwait(url: String, title: String, refetchAfterSuccess: Boolean = true): StrResponse {
return StrResponse( return StrResponse(
url, url,
SourceVerificationHelp.getVerificationResult(getSource(), url, title, true) SourceVerificationHelp.getVerificationResult(getSource(), url, title, true, refetchAfterSuccess)
) )
} }
@@ -30,7 +30,8 @@ object SourceVerificationHelp {
source: BaseSource?, source: BaseSource?,
url: String, url: String,
title: String, title: String,
useBrowser: Boolean useBrowser: Boolean,
refetchAfterSuccess: Boolean = true
): String { ): String {
source source
?: throw NoStackTraceException("getVerificationResult parameter source cannot be null") ?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
@@ -45,7 +46,7 @@ object SourceVerificationHelp {
IntentData.put(getVerificationResultKey(source), Thread.currentThread()) IntentData.put(getVerificationResultKey(source), Thread.currentThread())
} }
} else { } else {
startBrowser(source, url, title, true) startBrowser(source, url, title, true, refetchAfterSuccess)
} }
var waitUserInput = false var waitUserInput = false
@@ -72,7 +73,8 @@ object SourceVerificationHelp {
source: BaseSource?, source: BaseSource?,
url: String, url: String,
title: String, title: String,
saveResult: Boolean? = false saveResult: Boolean? = false,
refetchAfterSuccess: Boolean? = true
) { ) {
source ?: throw NoStackTraceException("startBrowser parameter source cannot be null") source ?: throw NoStackTraceException("startBrowser parameter source cannot be null")
appCtx.startActivity<WebViewActivity> { appCtx.startActivity<WebViewActivity> {
@@ -81,6 +83,7 @@ object SourceVerificationHelp {
putExtra("sourceOrigin", source.getKey()) putExtra("sourceOrigin", source.getKey())
putExtra("sourceName", source.getTag()) putExtra("sourceName", source.getTag())
putExtra("sourceVerificationEnable", saveResult) putExtra("sourceVerificationEnable", saveResult)
putExtra("refetchAfterSuccess", refetchAfterSuccess)
IntentData.put(url, source.getHeaderMap(true)) IntentData.put(url, source.getHeaderMap(true))
IntentData.put(getVerificationResultKey(source), Thread.currentThread()) IntentData.put(getVerificationResultKey(source), Thread.currentThread())
} }
@@ -193,8 +193,10 @@ class AnalyzeUrl(
baseUrl = it baseUrl = it
} }
if (urlNoOption.length != ruleUrl.length) { if (urlNoOption.length != ruleUrl.length) {
GSON.fromJsonObject<UrlOption>(ruleUrl.substring(urlMatcher.end())).getOrNull() val optionStr = ruleUrl.substring(urlMatcher.end())
GSON.fromJsonObject<UrlOption>(optionStr).getOrNull()
?.let { option -> ?.let { option ->
this.optionStr = optionStr
option.getMethod()?.let { option.getMethod()?.let {
if (it.equals("POST", true)) method = RequestMethod.POST if (it.equals("POST", true)) method = RequestMethod.POST
} }
@@ -82,7 +82,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
R.id.menu_copy_url -> sendToClip(viewModel.baseUrl) R.id.menu_copy_url -> sendToClip(viewModel.baseUrl)
R.id.menu_ok -> { R.id.menu_ok -> {
if (viewModel.sourceVerificationEnable) { if (viewModel.sourceVerificationEnable) {
viewModel.saveVerificationResult(intent) { viewModel.saveVerificationResult(binding.webView) {
finish() finish()
} }
} else { } else {
@@ -224,7 +224,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
if (it == "true") { if (it == "true") {
isCloudflareChallenge = true isCloudflareChallenge = true
} else if (isCloudflareChallenge && viewModel.sourceVerificationEnable) { } else if (isCloudflareChallenge && viewModel.sourceVerificationEnable) {
viewModel.saveVerificationResult(intent) { viewModel.saveVerificationResult(binding.webView) {
finish() finish()
} }
} }
@@ -5,6 +5,7 @@ import android.content.Intent
import android.net.Uri import android.net.Uri
import android.util.Base64 import android.util.Base64
import android.webkit.URLUtil import android.webkit.URLUtil
import android.webkit.WebView
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
@@ -22,14 +23,17 @@ import io.legado.app.utils.isContentScheme
import io.legado.app.utils.printOnDebug import io.legado.app.utils.printOnDebug
import io.legado.app.utils.toastOnUi import io.legado.app.utils.toastOnUi
import io.legado.app.utils.writeBytes import io.legado.app.utils.writeBytes
import org.apache.commons.text.StringEscapeUtils
import java.io.File import java.io.File
import java.util.Date import java.util.Date
class WebViewModel(application: Application) : BaseViewModel(application) { class WebViewModel(application: Application) : BaseViewModel(application) {
var intent: Intent? = null
var baseUrl: String = "" var baseUrl: String = ""
var html: String? = null var html: String? = null
val headerMap: HashMap<String, String> = hashMapOf() val headerMap: HashMap<String, String> = hashMapOf()
var sourceVerificationEnable: Boolean = false var sourceVerificationEnable: Boolean = false
var refetchAfterSuccess: Boolean = true
var sourceOrigin: String = "" var sourceOrigin: String = ""
fun initData( fun initData(
@@ -37,10 +41,12 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
success: () -> Unit success: () -> Unit
) { ) {
execute { execute {
this@WebViewModel.intent = intent
val url = intent.getStringExtra("url") val url = intent.getStringExtra("url")
?: throw NoStackTraceException("url不能为空") ?: throw NoStackTraceException("url不能为空")
sourceOrigin = intent.getStringExtra("sourceOrigin") ?: "" sourceOrigin = intent.getStringExtra("sourceOrigin") ?: ""
sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false) sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false)
refetchAfterSuccess = intent.getBooleanExtra("refetchAfterSuccess", true)
val headerMapF = IntentData.get<Map<String, String>>(url) val headerMapF = IntentData.get<Map<String, String>>(url)
val analyzeUrl = AnalyzeUrl(url, headerMapF = headerMapF) val analyzeUrl = AnalyzeUrl(url, headerMapF = headerMapF)
baseUrl = analyzeUrl.url baseUrl = analyzeUrl.url
@@ -89,10 +95,13 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
} }
} }
fun saveVerificationResult(intent: Intent, success: () -> Unit) { fun saveVerificationResult(webView: WebView, success: () -> Unit) {
if (!sourceVerificationEnable) {
execute { success.invoke() }
}
if (refetchAfterSuccess) {
execute { execute {
if (sourceVerificationEnable) { val url = intent!!.getStringExtra("url")!!
val url = intent.getStringExtra("url")!!
val source = appDb.bookSourceDao.getBookSource(sourceOrigin) val source = appDb.bookSourceDao.getBookSource(sourceOrigin)
html = AnalyzeUrl( html = AnalyzeUrl(
url, url,
@@ -100,10 +109,17 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
source = source source = source
).getStrResponseAwait(useWebView = false).body ).getStrResponseAwait(useWebView = false).body
SourceVerificationHelp.setResult(sourceOrigin, html ?: "") SourceVerificationHelp.setResult(sourceOrigin, html ?: "")
}
}.onSuccess {
success.invoke() success.invoke()
} }
} else {
webView.evaluateJavascript("document.documentElement.outerHTML") {
execute {
html = StringEscapeUtils.unescapeJson(it).trim('"')
SourceVerificationHelp.setResult(sourceOrigin, html ?: "")
success.invoke()
}
}
}
} }
} }