url添加js参数,解析url时执行,可在访问url时处理url

pull/416/head
gedoor 4 years ago
parent e3485cba92
commit 1ee4bf5570
  1. 5
      app/src/main/assets/updateLog.md
  2. 5
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  3. 3
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt
  4. 67
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -6,7 +6,10 @@
**2020/10/04** **2020/10/04**
* 更新时预下载10章 * 更新时预下载10章
* 支持更多分组 * 支持更多分组
* * url添加js参数,解析url时执行,可在访问url时处理url,例
```
https://www.baidu.com,{"js":"java.headerMap.put('xxx', 'yyy')"}
```
**2020/10/02** **2020/10/02**
* 优化规则解析 * 优化规则解析

@ -6,7 +6,6 @@ import io.legado.app.constant.AppConst.dateFormat
import io.legado.app.help.http.SSLHelper import io.legado.app.help.http.SSLHelper
import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.utils.EncodingDetect
import org.jsoup.Connection import org.jsoup.Connection
import org.jsoup.Jsoup import org.jsoup.Jsoup
import java.net.URLEncoder import java.net.URLEncoder
@ -22,7 +21,7 @@ interface JsExtensions {
*/ */
fun ajax(urlStr: String): String? { fun ajax(urlStr: String): String? {
return try { return try {
val analyzeUrl = AnalyzeUrl(urlStr, null, null, null, null, null) val analyzeUrl = AnalyzeUrl(urlStr)
val call = analyzeUrl.getResponse(urlStr) val call = analyzeUrl.getResponse(urlStr)
val response = call.execute() val response = call.execute()
response.body() response.body()
@ -33,7 +32,7 @@ interface JsExtensions {
fun connect(urlStr: String): Any { fun connect(urlStr: String): Any {
return try { return try {
val analyzeUrl = AnalyzeUrl(urlStr, null, null, null, null, null) val analyzeUrl = AnalyzeUrl(urlStr)
val call = analyzeUrl.getResponse(urlStr) val call = analyzeUrl.getResponse(urlStr)
val response = call.execute() val response = call.execute()
response response

@ -15,7 +15,6 @@ import java.util.regex.Pattern
import javax.script.SimpleBindings import javax.script.SimpleBindings
import kotlin.collections.HashMap import kotlin.collections.HashMap
/** /**
* Created by REFGD. * Created by REFGD.
* 统一解析接口 * 统一解析接口
@ -637,7 +636,7 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions {
*/ */
override fun ajax(urlStr: String): String? { override fun ajax(urlStr: String): String? {
return try { return try {
val analyzeUrl = AnalyzeUrl(urlStr, baseUrl = baseUrl, book = book) val analyzeUrl = AnalyzeUrl(urlStr, book = book)
val call = analyzeUrl.getResponse(urlStr) val call = analyzeUrl.getResponse(urlStr)
val response = call.execute() val response = call.execute()
response.body() response.body()

@ -34,30 +34,28 @@ import javax.script.SimpleBindings
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
class AnalyzeUrl( class AnalyzeUrl(
var ruleUrl: String, var ruleUrl: String,
key: String? = null, val key: String? = null,
page: Int? = null, val page: Int? = null,
speakText: String? = null, val speakText: String? = null,
speakSpeed: Int? = null, val speakSpeed: Int? = null,
headerMapF: Map<String, String>? = null, var baseUrl: String = "",
baseUrl: String? = null,
var useWebView: Boolean = false, var useWebView: Boolean = false,
val book: BaseBook? = null, val book: BaseBook? = null,
val chapter: BookChapter? = null val chapter: BookChapter? = null,
headerMapF: Map<String, String>? = null
) : JsExtensions { ) : JsExtensions {
companion object { companion object {
private val pagePattern = Pattern.compile("<(.*?)>") private val pagePattern = Pattern.compile("<(.*?)>")
private val jsonType = MediaType.parse("application/json; charset=utf-8") private val jsonType = MediaType.parse("application/json; charset=utf-8")
} }
private var baseUrl: String = "" var url: String = ""
lateinit var url: String
private set
private lateinit var urlHasQuery: String
val headerMap = HashMap<String, String>() val headerMap = HashMap<String, String>()
var body: String? = null
private lateinit var urlHasQuery: String
private var queryStr: String? = null private var queryStr: String? = null
private val fieldMap = LinkedHashMap<String, String>() private val fieldMap = LinkedHashMap<String, String>()
private var charset: String? = null private var charset: String? = null
private var body: String? = null
private var requestBody: RequestBody? = null private var requestBody: RequestBody? = null
private var method = RequestMethod.GET private var method = RequestMethod.GET
private val splitUrlRegex = Regex(",\\s*(?=\\{)") private val splitUrlRegex = Regex(",\\s*(?=\\{)")
@ -65,9 +63,7 @@ class AnalyzeUrl(
private var type: String? = null private var type: String? = null
init { init {
baseUrl?.let { baseUrl = baseUrl.split(splitUrlRegex, 1)[0]
this.baseUrl = it.split(splitUrlRegex, 1)[0]
}
headerMapF?.let { headerMapF?.let {
headerMap.putAll(it) headerMap.putAll(it)
if (it.containsKey("proxy")) { if (it.containsKey("proxy")) {
@ -76,19 +72,13 @@ class AnalyzeUrl(
} }
} }
//替换参数 //替换参数
analyzeJs(key, page, speakText, speakSpeed, book) analyzeJs()
replaceKeyPageJs(key, page, speakText, speakSpeed, book) replaceKeyPageJs()
//处理URL //处理URL
initUrl() initUrl()
} }
private fun analyzeJs( private fun analyzeJs() {
key: String?,
page: Int?,
speakText: String?,
speakSpeed: Int?,
book: BaseBook?,
) {
val ruleList = arrayListOf<String>() val ruleList = arrayListOf<String>()
var start = 0 var start = 0
var tmp: String var tmp: String
@ -116,12 +106,12 @@ class AnalyzeUrl(
ruleStr.startsWith("<js>") -> { ruleStr.startsWith("<js>") -> {
ruleStr = ruleStr.substring(4, ruleStr.lastIndexOf("<")) ruleStr = ruleStr.substring(4, ruleStr.lastIndexOf("<"))
ruleUrl = ruleUrl =
evalJS(ruleStr, ruleUrl, page, key, speakText, speakSpeed, book) as String evalJS(ruleStr, ruleUrl) as String
} }
ruleStr.startsWith("@js", true) -> { ruleStr.startsWith("@js", true) -> {
ruleStr = ruleStr.substring(4) ruleStr = ruleStr.substring(4)
ruleUrl = ruleUrl =
evalJS(ruleStr, ruleUrl, page, key, speakText, speakSpeed, book) as String evalJS(ruleStr, ruleUrl) as String
} }
else -> ruleUrl = ruleStr.replace("@result", ruleUrl) else -> ruleUrl = ruleStr.replace("@result", ruleUrl)
} }
@ -131,13 +121,7 @@ class AnalyzeUrl(
/** /**
* 替换关键字,页数,JS * 替换关键字,页数,JS
*/ */
private fun replaceKeyPageJs( private fun replaceKeyPageJs() {
key: String?,
page: Int?,
speakText: String?,
speakSpeed: Int?,
book: BaseBook?,
) {
//page //page
page?.let { page?.let {
val matcher = pagePattern.matcher(ruleUrl) val matcher = pagePattern.matcher(ruleUrl)
@ -215,7 +199,12 @@ class AnalyzeUrl(
body = if (it is String) it else GSON.toJson(it) body = if (it is String) it else GSON.toJson(it)
} }
option.webView?.let { 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( private fun evalJS(
jsStr: String, jsStr: String,
result: Any?, result: Any? = null
page: Int?,
key: String?,
speakText: String?,
speakSpeed: Int?,
book: BaseBook?,
): Any { ): Any {
val bindings = SimpleBindings() val bindings = SimpleBindings()
bindings["java"] = this bindings["java"] = this
@ -428,7 +412,8 @@ class AnalyzeUrl(
val webView: Any?, val webView: Any?,
val headers: Any?, val headers: Any?,
val body: Any?, val body: Any?,
val type: String? val type: String?,
val js: String?
) )
} }

Loading…
Cancel
Save