From 1ee4bf55705d5250e5be1890e3ac5d2df20b60ca Mon Sep 17 00:00:00 2001 From: gedoor Date: Tue, 6 Oct 2020 16:07:36 +0800 Subject: [PATCH] =?UTF-8?q?url=E6=B7=BB=E5=8A=A0js=E5=8F=82=E6=95=B0,?= =?UTF-8?q?=E8=A7=A3=E6=9E=90url=E6=97=B6=E6=89=A7=E8=A1=8C,=E5=8F=AF?= =?UTF-8?q?=E5=9C=A8=E8=AE=BF=E9=97=AEurl=E6=97=B6=E5=A4=84=E7=90=86url?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/updateLog.md | 5 +- .../java/io/legado/app/help/JsExtensions.kt | 5 +- .../app/model/analyzeRule/AnalyzeRule.kt | 3 +- .../app/model/analyzeRule/AnalyzeUrl.kt | 67 +++++++------------ 4 files changed, 33 insertions(+), 47 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 72b639197..36a49ecc0 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -6,7 +6,10 @@ **2020/10/04** * 更新时预下载10章 * 支持更多分组 -* +* url添加js参数,解析url时执行,可在访问url时处理url,例 +``` +https://www.baidu.com,{"js":"java.headerMap.put('xxx', 'yyy')"} +``` **2020/10/02** * 优化规则解析 diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index fc63fb8ab..7ba245bcd 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -6,7 +6,6 @@ import io.legado.app.constant.AppConst.dateFormat import io.legado.app.help.http.SSLHelper import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.utils.* -import io.legado.app.utils.EncodingDetect import org.jsoup.Connection import org.jsoup.Jsoup import java.net.URLEncoder @@ -22,7 +21,7 @@ interface JsExtensions { */ fun ajax(urlStr: String): String? { return try { - val analyzeUrl = AnalyzeUrl(urlStr, null, null, null, null, null) + val analyzeUrl = AnalyzeUrl(urlStr) val call = analyzeUrl.getResponse(urlStr) val response = call.execute() response.body() @@ -33,7 +32,7 @@ interface JsExtensions { fun connect(urlStr: String): Any { return try { - val analyzeUrl = AnalyzeUrl(urlStr, null, null, null, null, null) + val analyzeUrl = AnalyzeUrl(urlStr) val call = analyzeUrl.getResponse(urlStr) val response = call.execute() response diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 4d17aa16d..bfc0f9781 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -15,7 +15,6 @@ import java.util.regex.Pattern import javax.script.SimpleBindings import kotlin.collections.HashMap - /** * Created by REFGD. * 统一解析接口 @@ -637,7 +636,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { */ override fun ajax(urlStr: String): String? { return try { - val analyzeUrl = AnalyzeUrl(urlStr, baseUrl = baseUrl, book = book) + val analyzeUrl = AnalyzeUrl(urlStr, book = book) val call = analyzeUrl.getResponse(urlStr) val response = call.execute() response.body() diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index e274110a6..f9f7ec11e 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -34,30 +34,28 @@ import javax.script.SimpleBindings @SuppressLint("DefaultLocale") class AnalyzeUrl( var ruleUrl: String, - key: String? = null, - page: Int? = null, - speakText: String? = null, - speakSpeed: Int? = null, - headerMapF: Map? = null, - baseUrl: String? = null, + val key: String? = null, + val page: Int? = null, + val speakText: String? = null, + val speakSpeed: Int? = null, + var baseUrl: String = "", var useWebView: Boolean = false, val book: BaseBook? = null, - val chapter: BookChapter? = null + val chapter: BookChapter? = null, + headerMapF: Map? = null ) : JsExtensions { companion object { private val pagePattern = Pattern.compile("<(.*?)>") private val jsonType = MediaType.parse("application/json; charset=utf-8") } - private var baseUrl: String = "" - lateinit var url: String - private set - private lateinit var urlHasQuery: String + var url: String = "" val headerMap = HashMap() + var body: String? = null + private lateinit var urlHasQuery: String private var queryStr: String? = null private val fieldMap = LinkedHashMap() private var charset: String? = null - private var body: String? = null private var requestBody: RequestBody? = null private var method = RequestMethod.GET private val splitUrlRegex = Regex(",\\s*(?=\\{)") @@ -65,9 +63,7 @@ class AnalyzeUrl( private var type: String? = null init { - baseUrl?.let { - this.baseUrl = it.split(splitUrlRegex, 1)[0] - } + baseUrl = baseUrl.split(splitUrlRegex, 1)[0] headerMapF?.let { headerMap.putAll(it) if (it.containsKey("proxy")) { @@ -76,19 +72,13 @@ class AnalyzeUrl( } } //替换参数 - analyzeJs(key, page, speakText, speakSpeed, book) - replaceKeyPageJs(key, page, speakText, speakSpeed, book) + analyzeJs() + replaceKeyPageJs() //处理URL initUrl() } - private fun analyzeJs( - key: String?, - page: Int?, - speakText: String?, - speakSpeed: Int?, - book: BaseBook?, - ) { + private fun analyzeJs() { val ruleList = arrayListOf() var start = 0 var tmp: String @@ -116,12 +106,12 @@ class AnalyzeUrl( ruleStr.startsWith("") -> { ruleStr = ruleStr.substring(4, ruleStr.lastIndexOf("<")) ruleUrl = - evalJS(ruleStr, ruleUrl, page, key, speakText, speakSpeed, book) as String + evalJS(ruleStr, ruleUrl) as String } ruleStr.startsWith("@js", true) -> { ruleStr = ruleStr.substring(4) ruleUrl = - evalJS(ruleStr, ruleUrl, page, key, speakText, speakSpeed, book) as String + evalJS(ruleStr, ruleUrl) as String } else -> ruleUrl = ruleStr.replace("@result", ruleUrl) } @@ -131,13 +121,7 @@ class AnalyzeUrl( /** * 替换关键字,页数,JS */ - private fun replaceKeyPageJs( - key: String?, - page: Int?, - speakText: String?, - speakSpeed: Int?, - book: BaseBook?, - ) { + private fun replaceKeyPageJs() { //page page?.let { val matcher = pagePattern.matcher(ruleUrl) @@ -215,7 +199,12 @@ class AnalyzeUrl( body = if (it is String) it else GSON.toJson(it) } option.webView?.let { - useWebView = it.toString().isNotEmpty() + if (it.toString().isNotEmpty()) { + useWebView = true + } + } + option.js?.let { + evalJS(it) } } } @@ -271,12 +260,7 @@ class AnalyzeUrl( */ private fun evalJS( jsStr: String, - result: Any?, - page: Int?, - key: String?, - speakText: String?, - speakSpeed: Int?, - book: BaseBook?, + result: Any? = null ): Any { val bindings = SimpleBindings() bindings["java"] = this @@ -428,7 +412,8 @@ class AnalyzeUrl( val webView: Any?, val headers: Any?, val body: Any?, - val type: String? + val type: String?, + val js: String? ) }