feat:支持更多验证方式

This commit is contained in:
Xwite
2022-05-05 06:06:20 +08:00
parent 8cab8b77f7
commit 7e74c3bd4f
7 changed files with 109 additions and 42 deletions
+3
View File
@@ -67,6 +67,9 @@ java.webView(html: String?, url: String?, js: String?): String
* @param url 要打开的链接
* @param title 浏览器的标题
java.startBrowser(url: String, title: String)
* 使用内置浏览器打开链接,并等待网页结果 .body()获取网页内容
java.startBrowserAwait(url: String, title: String): StrResponse
```
* 调试
```
+1 -1
View File
@@ -13,7 +13,7 @@
**2022/05/04**
* js添加 getVerificationCode
* js添加 getVerificationCode startBrowserAwait
**2022/05/02**
@@ -15,13 +15,10 @@ import io.legado.app.help.http.*
import io.legado.app.model.Debug
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.model.analyzeRule.QueryTTF
import io.legado.app.ui.association.VerificationCodeActivity
import io.legado.app.ui.browser.WebViewActivity
import io.legado.app.utils.*
import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import org.jsoup.Connection
import org.jsoup.Jsoup
import splitties.init.appCtx
@@ -135,44 +132,26 @@ interface JsExtensions {
}
/**
* 使用内置浏览器打开链接,可用于获取验证码 手动验证网站防爬
* 使用内置浏览器打开链接,手动验证网站防爬
* @param url 要打开的链接
* @param title 浏览器页面的标题
*/
fun startBrowser(url: String, title: String) {
appCtx.startActivity<WebViewActivity> {
putExtra("title", title)
putExtra("url", url)
IntentData.put(url, getSource()?.getHeaderMap(true))
}
SourceVerificationHelp.startBrowser(getSource(), url, title)
}
/**
* 打开验证码对话框,等待输入验证码
* 使用内置浏览器打开链接,并等待网页结果
*/
fun startBrowserAwait(url: String, title: String): StrResponse {
return StrResponse(url, SourceVerificationHelp.getVerificationResult(getSource(), url, title, true))
}
/**
* 打开图片验证码对话框,等待返回验证结果
*/
fun getVerificationCode(imageUrl: String): String {
return runBlocking {
val key = "${getSource()?.getKey() ?: ""}_verificationCode"
CacheManager.delete(key)
appCtx.startActivity<VerificationCodeActivity> {
putExtra("imageUrl", imageUrl)
putExtra("sourceOrigin", getSource()?.getKey())
}
var waitUserInput: Boolean = false
while(CacheManager.get(key) == null) {
if (!waitUserInput) {
log("等待输入验证码...")
waitUserInput = true
}
}
CacheManager.get(key)!!.let {
if (it.isBlank()) {
throw NoStackTraceException("未输入验证码或者验证码为空")
} else {
it
}
}
}
return SourceVerificationHelp.getVerificationResult(getSource(), imageUrl, "", false)
}
/**
@@ -0,0 +1,69 @@
package io.legado.app.help
import io.legado.app.utils.startActivity
import io.legado.app.constant.AppLog
import io.legado.app.exception.NoStackTraceException
import io.legado.app.data.entities.BaseSource
import io.legado.app.ui.browser.WebViewActivity
import io.legado.app.ui.association.VerificationCodeActivity
import splitties.init.appCtx
import kotlinx.coroutines.runBlocking
object SourceVerificationHelp {
/**
* 获取书源验证结果
* 图片验证码 防爬 滑动验证码 点击字符 等等
*/
fun getVerificationResult(source: BaseSource?, url: String, title: String, useBrowser: Boolean): String {
source ?: throw NoStackTraceException("getVerificationResult parameter source cannot be null")
return runBlocking {
val key = "${source?.getKey() ?: ""}_verificationResult"
CacheManager.delete(key)
if (!useBrowser) {
appCtx.startActivity<VerificationCodeActivity> {
putExtra("imageUrl", url)
putExtra("sourceOrigin", source?.getKey())
}
} else {