From 52d0ba8b6027cae61a29e84042e4c9b5ff23f7f8 Mon Sep 17 00:00:00 2001 From: kunfei Date: Sun, 16 Oct 2022 22:23:40 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/model/webBook/WebBook.kt | 15 ++++++++++++++- .../legado/app/ui/book/info/BookInfoViewModel.kt | 7 +------ .../legado/app/ui/book/read/ReadBookViewModel.kt | 13 +------------ .../java/io/legado/app/ui/main/MainViewModel.kt | 7 +------ 4 files changed, 17 insertions(+), 25 deletions(-) diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index 32e8cf21b..5d3d38e1c 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -1,5 +1,6 @@ package io.legado.app.model.webBook +import io.legado.app.constant.AppLog import io.legado.app.data.entities.Book import io.legado.app.data.entities.BookChapter import io.legado.app.data.entities.BookSource @@ -9,6 +10,7 @@ import io.legado.app.help.book.getBookType import io.legado.app.help.coroutine.Coroutine import io.legado.app.help.http.StrResponse import io.legado.app.model.Debug +import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.RuleData import kotlinx.coroutines.CoroutineScope @@ -179,19 +181,30 @@ object WebBook { scope: CoroutineScope, bookSource: BookSource, book: Book, + runPerJs: Boolean = false, context: CoroutineContext = Dispatchers.IO ): Coroutine> { return Coroutine.async(scope, context) { - getChapterListAwait(bookSource, book).getOrThrow() + getChapterListAwait(bookSource, book, runPerJs).getOrThrow() } } suspend fun getChapterListAwait( bookSource: BookSource, book: Book, + runPerJs: Boolean = false ): Result> { book.type = bookSource.getBookType() return kotlin.runCatching { + val preUpdateJs = bookSource.ruleToc?.preUpdateJs + if (runPerJs && !preUpdateJs.isNullOrBlank()) { + kotlin.runCatching { + AnalyzeRule(book, bookSource).evalJS(preUpdateJs) + }.onFailure { + AppLog.put("执行preUpdateJs规则失败 书源:${bookSource.bookSourceName}", it) + throw it + } + } if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { BookChapterList.analyzeChapterList( bookSource = bookSource, diff --git a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt index cf051edff..250956564 100644 --- a/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/info/BookInfoViewModel.kt @@ -23,7 +23,6 @@ import io.legado.app.help.coroutine.Coroutine import io.legado.app.lib.webdav.ObjectNotFoundException import io.legado.app.model.BookCover import io.legado.app.model.ReadBook -import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.localBook.LocalBook import io.legado.app.model.remote.RemoteBookWebDav import io.legado.app.model.webBook.WebBook @@ -191,11 +190,7 @@ class BookInfoViewModel(application: Application) : BaseViewModel(application) { } else { bookSource?.let { bookSource -> val oldBook = book.copy() - val preUpdateJs = bookSource.ruleToc?.preUpdateJs - if (!preUpdateJs.isNullOrBlank()) { - AnalyzeRule(book, bookSource).evalJS(preUpdateJs) - } - WebBook.getChapterList(this, bookSource, book) + WebBook.getChapterList(this, bookSource, book, true) .onSuccess(IO) { if (inBookshelf) { if (oldBook.bookUrl == book.bookUrl) { diff --git a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt index 1054936e1..2564ffcba 100644 --- a/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/book/read/ReadBookViewModel.kt @@ -25,7 +25,6 @@ import io.legado.app.help.config.AppConfig import io.legado.app.help.coroutine.Coroutine import io.legado.app.model.ReadAloud import io.legado.app.model.ReadBook -import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.localBook.LocalBook import io.legado.app.model.webBook.WebBook import io.legado.app.service.BaseReadAloudService @@ -154,17 +153,7 @@ class ReadBookViewModel(application: Application) : BaseViewModel(application) { ReadBook.bookSource?.let { viewModelScope.launch(IO) { val oldBook = book.copy() - val preUpdateJs = it.ruleToc?.preUpdateJs - if (!preUpdateJs.isNullOrBlank()) { - val source = it - kotlin.runCatching { - AnalyzeRule(book, it).evalJS(preUpdateJs) - }.onFailure { - ReadBook.upMsg("执行preUpdateJs规则失败") - AppLog.put("执行preUpdateJs规则失败 书源:${source.bookSourceName}", it) - } - } - WebBook.getChapterList(viewModelScope, it, book) + WebBook.getChapterList(viewModelScope, it, book, true) .onSuccess(IO) { cList -> if (oldBook.bookUrl == book.bookUrl) { appDb.bookDao.update(book) diff --git a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt index 98e636be1..485bd6997 100644 --- a/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/main/MainViewModel.kt @@ -19,7 +19,6 @@ import io.legado.app.help.book.removeType import io.legado.app.help.config.AppConfig import io.legado.app.help.config.LocalConfig import io.legado.app.model.CacheBook -import io.legado.app.model.analyzeRule.AnalyzeRule import io.legado.app.model.webBook.WebBook import io.legado.app.service.CacheBookService import io.legado.app.utils.postEvent @@ -123,14 +122,10 @@ class MainViewModel(application: Application) : BaseViewModel(application) { upTocAdd(bookUrl) execute(context = upTocPool) { val oldBook = book.copy() - val preUpdateJs = source.ruleToc?.preUpdateJs - if (!preUpdateJs.isNullOrBlank()) { - AnalyzeRule(book, source).evalJS(preUpdateJs) - } if (book.tocUrl.isBlank()) { WebBook.getBookInfoAwait(source, book) } - val toc = WebBook.getChapterListAwait(source, book).getOrThrow() + val toc = WebBook.getChapterListAwait(source, book, true).getOrThrow() book.removeType(BookType.updateError) if (book.bookUrl == bookUrl) { appDb.bookDao.update(book)