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**
* 更新时预下载10章
* 支持更多分组
*
* url添加js参数,解析url时执行,可在访问url时处理url,例
```
https://www.baidu.com,{"js":"java.headerMap.put('xxx', 'yyy')"}
```
**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.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

@ -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()

@ -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<String, String>? = 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<String, String>? = 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<String, String>()
var body: String? = null
private lateinit var urlHasQuery: String
private var queryStr: String? = null
private val fieldMap = LinkedHashMap<String, String>()
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<String>()
var start = 0
var tmp: String
@ -116,12 +106,12 @@ class AnalyzeUrl(
ruleStr.startsWith("<js>") -> {
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?
)
}

Loading…
Cancel
Save