pull/32/head
kunfei 5 years ago
parent 60d4d3ade6
commit e97951b9e1
  1. 1
      app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt
  2. 50
      app/src/main/java/io/legado/app/help/JsExtensions.java
  3. 38
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@ -23,7 +23,6 @@ data class ExploreRule(
if (a.isNotBlank()) { if (a.isNotBlank()) {
try { try {
if (it.startsWith("<js>", false)) { if (it.startsWith("<js>", false)) {
val bindings = SimpleBindings() val bindings = SimpleBindings()
bindings["baseUrl"] = baseUrl bindings["baseUrl"] = baseUrl
a = AppConst.SCRIPT_ENGINE.eval( a = AppConst.SCRIPT_ENGINE.eval(

@ -0,0 +1,50 @@
package io.legado.app.help;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import io.legado.app.model.analyzeRule.AnalyzeUrl;
import io.legado.app.utils.Encoder;
import io.legado.app.utils.StringUtils;
public class JsExtensions {
/**
* js实现跨域访问,不能删
*/
public String ajax(String urlStr) {
try {
AnalyzeUrl analyzeUrl = new AnalyzeUrl(urlStr, null, null, null, null, null);
/* Response response = analyzeUrl.getResponseAsync().await();
return response.body().toString();*/
} catch (Exception e) {
return e.getLocalizedMessage();
}
return null;
}
/**
* js实现解码,不能删
*/
public String base64Decoder(String str) {
return Encoder.INSTANCE.base64Decoder(str);
}
/**
* 章节数转数字
*/
public String toNumChapter(String s) {
if (s == null) {
return null;
}
Pattern pattern = Pattern.compile("(第)(.+?)(章)");
Matcher matcher = pattern.matcher(s);
if (matcher.find()) {
return matcher.group(1) + StringUtils.INSTANCE.stringToInt(matcher.group(2)) + matcher.group(3);
} else {
return s;
}
}
}

@ -5,6 +5,7 @@ import androidx.annotation.Keep
import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.AppConst.SCRIPT_ENGINE
import io.legado.app.constant.Pattern.JS_PATTERN import io.legado.app.constant.Pattern.JS_PATTERN
import io.legado.app.data.entities.BaseBook import io.legado.app.data.entities.BaseBook
import io.legado.app.help.JsExtensions
import io.legado.app.utils.* import io.legado.app.utils.*
import java.util.* import java.util.*
import java.util.regex.Pattern import java.util.regex.Pattern
@ -515,46 +516,13 @@ class AnalyzeRule(private var book: BaseBook? = null) {
@Throws(Exception::class) @Throws(Exception::class)
private fun evalJS(jsStr: String, result: Any?): Any { private fun evalJS(jsStr: String, result: Any?): Any {
val bindings = SimpleBindings() val bindings = SimpleBindings()
bindings["java"] = this bindings["book"] = this
bindings["java"] = JsExtensions()
bindings["result"] = result bindings["result"] = result
bindings["baseUrl"] = baseUrl bindings["baseUrl"] = baseUrl
return SCRIPT_ENGINE.eval(jsStr, bindings) return SCRIPT_ENGINE.eval(jsStr, bindings)
} }
/**
* js实现跨域访问,不能删
*/
suspend fun ajax(urlStr: String): String? {
return try {
val analyzeUrl = AnalyzeUrl(urlStr)
val response = analyzeUrl.getResponseAsync().await()
response.body()
} catch (e: Exception) {
e.localizedMessage
}
}
/**
* js实现解码,不能删
*/
fun base64Decoder(str: String): String {
return Encoder.base64Decoder(str)
}
/**
* 章节数转数字
*/
fun toNumChapter(s: String?): String? {
if (s == null) {
return null
}
val pattern = Pattern.compile("(第)(.+?)(章)")
val matcher = pattern.matcher(s)
return if (matcher.find()) {
matcher.group(1) + StringUtils.stringToInt(matcher.group(2)) + matcher.group(3)
} else s
}
companion object { companion object {
private val putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE) private val putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE)
private val getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE) private val getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE)

Loading…
Cancel
Save