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 d196e18fa..f9ed9230f 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 @@ -324,17 +324,37 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { */ private fun replaceRegex(result: String, rule: SourceRule): String { var vResult = result - if (rule.replaceRegex.isNotEmpty()) { + val stringBuffer = StringBuffer() + val evalMatcher = replacePattern.matcher(rule.replaceRegex) + while (evalMatcher.find()) { + val jsEval = evalMatcher.group().let { + if (it.startsWith("@get:", true)) { + get(it.substring(6, it.lastIndex)) + } else { + evalJS(it.substring(2, it.length - 2), result) + } + } ?: "" + if (jsEval is String) { + evalMatcher.appendReplacement(stringBuffer, jsEval) + } else if (jsEval is Double && jsEval % 1.0 == 0.0) { + evalMatcher.appendReplacement(stringBuffer, String.format("%.0f", jsEval)) + } else { + evalMatcher.appendReplacement(stringBuffer, jsEval.toString()) + } + } + evalMatcher.appendTail(stringBuffer) + val replaceRegex = stringBuffer.toString() + if (replaceRegex.isNotEmpty()) { vResult = if (rule.replaceFirst) { - val pattern = Pattern.compile(rule.replaceRegex) + val pattern = Pattern.compile(replaceRegex) val matcher = pattern.matcher(vResult) if (matcher.find()) { - matcher.group(0)!!.replaceFirst(rule.replaceRegex.toRegex(), rule.replacement) + matcher.group(0)!!.replaceFirst(replaceRegex.toRegex(), rule.replacement) } else { "" } } else { - vResult.replace(rule.replaceRegex.toRegex(), rule.replacement) + vResult.replace(replaceRegex.toRegex(), rule.replacement) } } return vResult @@ -644,6 +664,10 @@ class AnalyzeRule(var book: BaseBook? = null) : JsExtensions { "@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}|\\$\\d{1,2}", Pattern.CASE_INSENSITIVE ) + private val replacePattern = Pattern.compile( + "@get:\\{[^}]+?\\}|\\{\\{[\\w\\W]*?\\}\\}", + Pattern.CASE_INSENSITIVE + ) } } 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 396df397e..067690afb 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 @@ -150,7 +150,7 @@ class AnalyzeUrl( //js if (ruleUrl.contains("{{") && ruleUrl.contains("}}")) { var jsEval: Any - val sb = StringBuffer(ruleUrl.length) + val sb = StringBuffer() val simpleBindings = SimpleBindings() simpleBindings["java"] = this simpleBindings["baseUrl"] = baseUrl