From e97951b9e1b4d835397261756d4594ee154b88b0 Mon Sep 17 00:00:00 2001 From: kunfei Date: Wed, 4 Sep 2019 10:41:54 +0800 Subject: [PATCH] up --- .../app/data/entities/rule/ExploreRule.kt | 1 - .../java/io/legado/app/help/JsExtensions.java | 50 +++++++++++++++++++ .../app/model/analyzeRule/AnalyzeRule.kt | 38 ++------------ 3 files changed, 53 insertions(+), 36 deletions(-) create mode 100644 app/src/main/java/io/legado/app/help/JsExtensions.java diff --git a/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt index 921210083..7337e0245 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/ExploreRule.kt @@ -23,7 +23,6 @@ data class ExploreRule( if (a.isNotBlank()) { try { if (it.startsWith("", false)) { - val bindings = SimpleBindings() bindings["baseUrl"] = baseUrl a = AppConst.SCRIPT_ENGINE.eval( diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.java b/app/src/main/java/io/legado/app/help/JsExtensions.java new file mode 100644 index 000000000..93dc3ba92 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/JsExtensions.java @@ -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; + } + } + +} 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 19bb80eec..c3a5a36fd 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 @@ -5,6 +5,7 @@ import androidx.annotation.Keep import io.legado.app.constant.AppConst.SCRIPT_ENGINE import io.legado.app.constant.Pattern.JS_PATTERN import io.legado.app.data.entities.BaseBook +import io.legado.app.help.JsExtensions import io.legado.app.utils.* import java.util.* import java.util.regex.Pattern @@ -515,46 +516,13 @@ class AnalyzeRule(private var book: BaseBook? = null) { @Throws(Exception::class) private fun evalJS(jsStr: String, result: Any?): Any { val bindings = SimpleBindings() - bindings["java"] = this + bindings["book"] = this + bindings["java"] = JsExtensions() bindings["result"] = result bindings["baseUrl"] = baseUrl 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 { private val putPattern = Pattern.compile("@put:(\\{[^}]+?\\})", Pattern.CASE_INSENSITIVE) private val getPattern = Pattern.compile("@get:\\{([^}]+?)\\}", Pattern.CASE_INSENSITIVE)