mirror of
https://github.com/gedoor/legado.git
synced 2025-08-10 00:52:30 +00:00
startBrowserAwait 添加 refetchAfterSuccess 参数,为 true 时返回的页面源码为用户看见的页面内容
This commit is contained in:
@@ -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(
|
||||
url,
|
||||
SourceVerificationHelp.getVerificationResult(getSource(), url, title, true)
|
||||
SourceVerificationHelp.getVerificationResult(getSource(), url, title, true, refetchAfterSuccess)
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -30,7 +30,8 @@ object SourceVerificationHelp {
|
||||
source: BaseSource?,
|
||||
url: String,
|
||||
title: String,
|
||||
useBrowser: Boolean
|
||||
useBrowser: Boolean,
|
||||
refetchAfterSuccess: Boolean = true
|
||||
): String {
|
||||
source
|
||||
?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
|
||||
@@ -45,7 +46,7 @@ object SourceVerificationHelp {
|
||||
IntentData.put(getVerificationResultKey(source), Thread.currentThread())
|
||||
}
|
||||
} else {
|
||||
startBrowser(source, url, title, true)
|
||||
startBrowser(source, url, title, true, refetchAfterSuccess)
|
||||
}
|
||||
|
||||
var waitUserInput = false
|
||||
@@ -72,7 +73,8 @@ object SourceVerificationHelp {
|
||||
source: BaseSource?,
|
||||
url: String,
|
||||
title: String,
|
||||
saveResult: Boolean? = false
|
||||
saveResult: Boolean? = false,
|
||||
refetchAfterSuccess: Boolean? = true
|
||||
) {
|
||||
source ?: throw NoStackTraceException("startBrowser parameter source cannot be null")
|
||||
appCtx.startActivity<WebViewActivity> {
|
||||
@@ -81,6 +83,7 @@ object SourceVerificationHelp {
|
||||
putExtra("sourceOrigin", source.getKey())
|
||||
putExtra("sourceName", source.getTag())
|
||||
putExtra("sourceVerificationEnable", saveResult)
|
||||
putExtra("refetchAfterSuccess", refetchAfterSuccess)
|
||||
IntentData.put(url, source.getHeaderMap(true))
|
||||
IntentData.put(getVerificationResultKey(source), Thread.currentThread())
|
||||
}
|
||||
|
||||
@@ -193,8 +193,10 @@ class AnalyzeUrl(
|
||||
baseUrl = it
|
||||
}
|
||||
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 ->
|
||||
this.optionStr = optionStr
|
||||
option.getMethod()?.let {
|
||||
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_ok -> {
|
||||
if (viewModel.sourceVerificationEnable) {
|
||||
viewModel.saveVerificationResult(intent) {
|
||||
viewModel.saveVerificationResult(binding.webView) {
|
||||
finish()
|
||||
}
|
||||
} else {
|
||||
@@ -224,7 +224,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
|
||||
if (it == "true") {
|
||||
isCloudflareChallenge = true
|
||||
} else if (isCloudflareChallenge && viewModel.sourceVerificationEnable) {
|
||||
viewModel.saveVerificationResult(intent) {
|
||||
viewModel.saveVerificationResult(binding.webView) {
|
||||
finish()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ import android.content.Intent
|
||||
import android.net.Uri
|
||||
import android.util.Base64
|
||||
import android.webkit.URLUtil
|
||||
import android.webkit.WebView
|
||||
import androidx.documentfile.provider.DocumentFile
|
||||
import io.legado.app.base.BaseViewModel
|
||||
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.toastOnUi
|
||||
import io.legado.app.utils.writeBytes
|
||||
import org.apache.commons.text.StringEscapeUtils
|
||||
import java.io.File
|
||||
import java.util.Date
|
||||
|
||||
class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
var intent: Intent? = null
|
||||
var baseUrl: String = ""
|
||||
var html: String? = null
|
||||
val headerMap: HashMap<String, String> = hashMapOf()
|
||||
var sourceVerificationEnable: Boolean = false
|
||||
var refetchAfterSuccess: Boolean = true
|
||||
var sourceOrigin: String = ""
|
||||
|
||||
fun initData(
|
||||
@@ -37,10 +41,12 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
success: () -> Unit
|
||||
) {
|
||||
execute {
|
||||
this@WebViewModel.intent = intent
|
||||
val url = intent.getStringExtra("url")
|
||||
?: throw NoStackTraceException("url不能为空")
|
||||
sourceOrigin = intent.getStringExtra("sourceOrigin") ?: ""
|
||||
sourceVerificationEnable = intent.getBooleanExtra("sourceVerificationEnable", false)
|
||||
refetchAfterSuccess = intent.getBooleanExtra("refetchAfterSuccess", true)
|
||||
val headerMapF = IntentData.get<Map<String, String>>(url)
|
||||
val analyzeUrl = AnalyzeUrl(url, headerMapF = headerMapF)
|
||||
baseUrl = analyzeUrl.url
|
||||
@@ -89,10 +95,13 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
}
|
||||
}
|
||||
|
||||
fun saveVerificationResult(intent: Intent, success: () -> Unit) {
|
||||
execute {
|
||||
if (sourceVerificationEnable) {
|
||||
val url = intent.getStringExtra("url")!!
|
||||
fun saveVerificationResult(webView: WebView, success: () -> Unit) {
|
||||
if (!sourceVerificationEnable) {
|
||||
execute { success.invoke() }
|
||||
}
|
||||
if (refetchAfterSuccess) {
|
||||
execute {
|
||||
val url = intent!!.getStringExtra("url")!!
|
||||
val source = appDb.bookSourceDao.getBookSource(sourceOrigin)
|
||||
html = AnalyzeUrl(
|
||||
url,
|
||||
@@ -100,9 +109,16 @@ class WebViewModel(application: Application) : BaseViewModel(application) {
|
||||
source = source
|
||||
).getStrResponseAwait(useWebView = false).body
|
||||
SourceVerificationHelp.setResult(sourceOrigin, html ?: "")
|
||||
success.invoke()
|
||||
}
|
||||
} else {
|
||||
webView.evaluateJavascript("document.documentElement.outerHTML") {
|
||||
execute {
|
||||
html = StringEscapeUtils.unescapeJson(it).trim('"')
|
||||
SourceVerificationHelp.setResult(sourceOrigin, html ?: "")
|
||||
success.invoke()
|
||||
}
|
||||
}
|
||||
}.onSuccess {
|
||||
success.invoke()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user