pull/1814/head
kunfei 3 years ago
parent 4759c0b8dd
commit 4dd58c1e98
  1. 2
      app/src/main/java/io/legado/app/data/entities/rule/ContentRule.kt
  2. 1
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 12
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt
  4. 34
      app/src/main/java/io/legado/app/utils/JsUtils.kt

@ -11,5 +11,5 @@ data class ContentRule(
var sourceRegex: String? = null,
var replaceRegex: String? = null, //替换规则
var imageStyle: String? = null, //默认大小居中,FULL最大宽度
var payAction: String? = null, //购买操作,url/js
var payAction: String? = null, //购买操作,js或者包含{{js}}的url
) : Parcelable

@ -460,6 +460,7 @@ class AnalyzeUrl(
*/
suspend fun getByteArrayAwait(): ByteArray {
val concurrentRecord = fetchStart()
@Suppress("RegExpRedundantEscape")
val dataUriFindResult = dataUriRegex.find(urlNoQuery)
@Suppress("BlockingMethodInNonBlockingContext")

@ -919,22 +919,16 @@ class ReadBookActivity : BaseReadBookActivity(),
if (payAction.isNullOrEmpty()) {
throw NoStackTraceException("no pay action")
}
if (payAction.isAbsUrl()) {
payAction
} else {
source.evalJS(payAction) {
put("book", book)
put("chapter", chapter)
}?.toString()
JsUtils.evalJs(payAction) {
it["book"] = book
it["chapter"] = chapter
}
}.onSuccess {
it?.let {
startActivity<WebViewActivity> {
putExtra("title", getString(R.string.chapter_pay))
putExtra("url", it)
IntentData.put(it, ReadBook.bookSource?.getHeaderMap(true))
}
}
}.onError {
toastOnUi(it.localizedMessage)
}

@ -0,0 +1,34 @@
package io.legado.app.utils
import io.legado.app.constant.AppConst.SCRIPT_ENGINE
import io.legado.app.constant.AppPattern.EXP_PATTERN
import javax.script.SimpleBindings
object JsUtils {
fun evalJs(js: String, bindingsFun: ((SimpleBindings) -> Unit)? = null): String {
val bindings = SimpleBindings()
bindingsFun?.invoke(bindings)
if (js.contains("{{") && js.contains("}}")) {
val sb = StringBuffer()
val expMatcher = EXP_PATTERN.matcher(js)
while (expMatcher.find()) {
val result = expMatcher.group(1)?.let {
SCRIPT_ENGINE.eval(it, bindings)
} ?: ""
if (result is String) {
expMatcher.appendReplacement(sb, result)
} else if (result is Double && result % 1.0 == 0.0) {
expMatcher.appendReplacement(sb, String.format("%.0f", result))
} else {
expMatcher.appendReplacement(sb, result.toString())
}
}
expMatcher.appendTail(sb)
return sb.toString()
}
return SCRIPT_ENGINE.eval(js, bindings).toString()
}
}
Loading…
Cancel
Save