Merge pull request #2215 from odomu/master

优化payAction
pull/2217/head
kunfei 2 years ago committed by GitHub
commit 3e7a038d29
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 3
      app/src/main/assets/help/ruleHelp.md
  2. 31
      app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt

@ -152,5 +152,4 @@ let options = {
``` ```
* 购买操作 * 购买操作
> 返回购买链接,可直接填写链接或者JavaScript > 可直接填写链接或者JavaScript,如果执行结果是字符串链接将会自动打开浏览器
> 可用变量 book chapter java(不支持AnalyzeRule函数) source

@ -36,6 +36,7 @@ import io.legado.app.lib.dialogs.selector
import io.legado.app.lib.theme.accentColor import io.legado.app.lib.theme.accentColor
import io.legado.app.model.ReadAloud import io.legado.app.model.ReadAloud
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.model.analyzeRule.AnalyzeRule
import io.legado.app.receiver.TimeBatteryReceiver import io.legado.app.receiver.TimeBatteryReceiver
import io.legado.app.service.BaseReadAloudService import io.legado.app.service.BaseReadAloudService
import io.legado.app.ui.about.AppLogDialog import io.legado.app.ui.about.AppLogDialog
@ -962,33 +963,43 @@ class ReadBookActivity : BaseReadBookActivity(),
} }
override fun payAction() { override fun payAction() {
Coroutine.async(this) { ReadBook.book?.let { book ->
val book = ReadBook.book ?: throw NoStackTraceException("no book") if (book.isLocalBook()) return
val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, ReadBook.durChapterIndex) val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, ReadBook.durChapterIndex)
?: throw NoStackTraceException("no chapter") if (chapter == null) {
toastOnUi("no chapter")
return
}
alert(R.string.chapter_pay) {
setMessage(chapter.title)
yesButton {
Coroutine.async {
val source = ReadBook.bookSource ?: throw NoStackTraceException("no book source") val source = ReadBook.bookSource ?: throw NoStackTraceException("no book source")
val payAction = source.getContentRule().payAction val payAction = source.getContentRule().payAction
if (payAction.isNullOrEmpty()) { if (payAction.isNullOrEmpty()) {
throw NoStackTraceException("no pay action") throw NoStackTraceException("no pay action")
} }
JsUtils.evalJs(payAction) { val analyzeRule = AnalyzeRule(book, source)
it["java"] = source analyzeRule.setBaseUrl(chapter.url)
it["source"] = source analyzeRule.chapter = chapter
it["book"] = book analyzeRule.evalJS(payAction).toString()
it["chapter"] = chapter
}
}.onSuccess { }.onSuccess {
if (it.isNotBlank()) { if (it.isNotBlank()) {
startActivity<WebViewActivity> { startActivity<WebViewActivity> {
putExtra("title", getString(R.string.chapter_pay)) putExtra("title", 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 {
AppLog.putDebug(it.localizedMessage)
toastOnUi(it.localizedMessage) toastOnUi(it.localizedMessage)
} }
} }
noButton()
}
}
}
/** /**
* 朗读按钮 * 朗读按钮

Loading…
Cancel
Save