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

@ -33,7 +33,7 @@ import splitties.init.appCtx
data class BookChapter( data class BookChapter(
var url: String = "", // 章节地址 var url: String = "", // 章节地址
var title: String = "", // 章节标题 var title: String = "", // 章节标题
var isVolume: Boolean = false, // 是否是卷名 var isVolume: Boolean = false, // 是否是卷名
var baseUrl: String = "", // 用来拼接相对url var baseUrl: String = "", // 用来拼接相对url
var bookUrl: String = "", // 书籍地址 var bookUrl: String = "", // 书籍地址
var index: Int = 0, // 章节序号 var index: Int = 0, // 章节序号

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

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

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