From c203e90160e8c4939fed98c5cb4a880ada37a35d Mon Sep 17 00:00:00 2001 From: Odomu <1374415225@qq.com> Date: Tue, 30 Aug 2022 10:00:02 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96payAction?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/help/ruleHelp.md | 3 +- .../app/ui/book/read/ReadBookActivity.kt | 55 +++++++++++-------- 2 files changed, 34 insertions(+), 24 deletions(-) diff --git a/app/src/main/assets/help/ruleHelp.md b/app/src/main/assets/help/ruleHelp.md index e6c1643a4..554509612 100644 --- a/app/src/main/assets/help/ruleHelp.md +++ b/app/src/main/assets/help/ruleHelp.md @@ -152,5 +152,4 @@ let options = { ``` * 购买操作 -> 返回购买链接,可直接填写链接或者JavaScript -> 可用变量 book chapter java(不支持AnalyzeRule函数) source \ No newline at end of file +> 可直接填写链接或者JavaScript,如果执行结果是字符串链接将会自动打开浏览器 \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt index 7fcab742b..81edc9ea7 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookActivity.kt @@ -36,6 +36,7 @@ import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.accentColor import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook +import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.receiver.TimeBatteryReceiver import io.legado.app.service.BaseReadAloudService import io.legado.app.ui.about.AppLogDialog @@ -962,31 +963,41 @@ class ReadBookActivity : BaseReadBookActivity(), } override fun payAction() { - Coroutine.async(this) { - val book = ReadBook.book ?: throw NoStackTraceException("no book") + ReadBook.book?.let { book -> + if (book.isLocalBook()) return val chapter = appDb.bookChapterDao.getChapter(book.bookUrl, ReadBook.durChapterIndex) - ?: throw NoStackTraceException("no chapter") - val source = ReadBook.bookSource ?: throw NoStackTraceException("no book source") - val payAction = source.getContentRule().payAction - if (payAction.isNullOrEmpty()) { - throw NoStackTraceException("no pay action") - } - JsUtils.evalJs(payAction) { - it["java"] = source - it["source"] = source - it["book"] = book - it["chapter"] = chapter - } - }.onSuccess { - if (it.isNotBlank()) { - startActivity { - putExtra("title", getString(R.string.chapter_pay)) - putExtra("url", it) - IntentData.put(it, ReadBook.bookSource?.getHeaderMap(true)) + 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 payAction = source.getContentRule().payAction + if (payAction.isNullOrEmpty()) { + throw NoStackTraceException("no pay action") + } + val analyzeRule = AnalyzeRule(book, source) + analyzeRule.setBaseUrl(chapter.url) + analyzeRule.chapter = chapter + analyzeRule.evalJS(payAction).toString() + }.onSuccess { + if (it.isNotBlank()) { + startActivity { + putExtra("title", R.string.chapter_pay) + putExtra("url", it) + IntentData.put(it, ReadBook.bookSource?.getHeaderMap(true)) + } + } + }.onError { + AppLog.putDebug(it.localizedMessage) + toastOnUi(it.localizedMessage) + } } + noButton() } - }.onError { - toastOnUi(it.localizedMessage) } }