去除刷新前重新获取目录url

添加刷新前js,功能更强大
This commit is contained in:
kunfei
2022-05-28 23:30:00 +08:00
parent 717f6b9e94
commit 4ae193cda3
16 changed files with 72 additions and 32 deletions
@@ -130,21 +130,43 @@ data class BookSource(
return bookSourceUrl.hashCode()
}
override fun equals(other: Any?) =
if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false
override fun equals(other: Any?): Boolean {
return if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false
}
fun getSearchRule() = ruleSearch ?: SearchRule()
fun getSearchRule(): SearchRule {
ruleSearch?.let { return it }
val rule = SearchRule()
ruleSearch = rule
return rule
}
fun getExploreRule() = ruleExplore ?: ExploreRule()
fun getExploreRule(): ExploreRule {
ruleExplore?.let { return it }
val rule = ExploreRule()
ruleExplore = rule
return rule
}
fun getBookInfoRule() = ruleBookInfo ?: BookInfoRule()
fun getBookInfoRule(): BookInfoRule {
ruleBookInfo?.let { return it }
val rule = BookInfoRule()
ruleBookInfo = rule
return rule
}
fun getTocRule() = ruleToc ?: TocRule()
fun getTocRule(): TocRule {
ruleToc?.let { return it }
val rule = TocRule()
ruleToc = rule
return rule
}
fun getContentRule() = ruleContent ?: ContentRule()
fun isReGetTocUrlOnRefresh(): Boolean {
return ruleBookInfo?.reGetTocUrlOnRefresh.isTrue()
fun getContentRule(): ContentRule {
ruleContent?.let { return it }
val rule = ContentRule()
ruleContent = rule
return rule
}
fun getDisPlayNameGroup(): String {
@@ -17,6 +17,5 @@ data class BookInfoRule(
var tocUrl: String? = null,
var wordCount: String? = null,
var canReName: String? = null,
var downloadUrls: String? = null,
var reGetTocUrlOnRefresh: String? = null
var downloadUrls: String? = null
) : Parcelable
@@ -5,6 +5,7 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class TocRule(
var preUpdateJs: String? = null,
var chapterList: String? = null,
var chapterName: String? = null,
var chapterUrl: String? = null,
@@ -634,7 +634,7 @@ class AnalyzeRule(
/**
* 执行JS
*/
fun evalJS(jsStr: String, result: Any?): Any? {
fun evalJS(jsStr: String, result: Any? = null): Any? {
val bindings = SimpleBindings()
bindings["java"] = this
bindings["cookie"] = CookieStore
@@ -277,16 +277,15 @@ object LocalBook {
val defaultBookTreeUri = AppConfig.defaultBookTreeUri
if (defaultBookTreeUri.isNullOrBlank()) throw NoStackTraceException("没有设置书籍保存位置!")
val treeUri = Uri.parse(defaultBookTreeUri)
var bookUrl: String = ""
if (treeUri.isContentScheme()) {
val bookUrl = if (treeUri.isContentScheme()) {
val treeDoc = DocumentFile.fromTreeUri(appCtx, treeUri)
var doc = treeDoc!!.findFile(fileName) ?: return false
bookUrl = doc.uri.toString()
val doc = treeDoc!!.findFile(fileName) ?: return false
doc.uri.toString()
} else {
val treeFile = File(treeUri.path!!)
val file = treeFile.getFile(fileName)
if (!file.exists()) return false
bookUrl = file.absolutePath
file.absolutePath
}
return appDb.bookDao.getBook(bookUrl) != null
}
@@ -2,8 +2,8 @@ package io.legado.app.ui.book.remote.manager
import android.net.Uri
import io.legado.app.constant.PreferKey
import io.legado.app.constant.AppPattern.bookFileRegex
import io.legado.app.constant.PreferKey
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.AppWebDav
import io.legado.app.help.config.AppConfig
@@ -12,7 +12,10 @@ import io.legado.app.lib.webdav.WebDavFile
import io.legado.app.model.localBook.LocalBook
import io.legado.app.ui.book.remote.RemoteBook
import io.legado.app.ui.book.remote.RemoteBookManager
import io.legado.app.utils.*
import io.legado.app.utils.NetworkUtils
import io.legado.app.utils.getPrefString
import io.legado.app.utils.isContentScheme
import io.legado.app.utils.readBytes
import kotlinx.coroutines.runBlocking
import splitties.init.appCtx
import java.io.File
@@ -96,7 +99,6 @@ object RemoteBookWebDav : RemoteBookManager() {
}
}.onFailure {
it.printStackTrace()
null
}.getOrNull()
}
@@ -269,12 +269,12 @@ class BookSourceEditActivity :
add(EditEntity("tocUrl", ir?.tocUrl, R.string.rule_toc_url))
add(EditEntity("canReName", ir?.canReName, R.string.rule_can_re_name))
add(EditEntity("downloadUrls", ir?.downloadUrls, R.string.download_url_rule))
add(EditEntity("reGetTocUrl", ir?.reGetTocUrlOnRefresh, R.string.rule_re_get_toc_url))
}
//目录页
val tr = source?.getTocRule()
tocEntities.clear()
tocEntities.apply {
add(EditEntity("preUpdateJs", tr?.preUpdateJs, R.string.pre_update_js))
add(EditEntity("chapterList", tr?.chapterList, R.string.rule_chapter_list))
add(EditEntity("chapterName", tr?.chapterName, R.string.rule_chapter_name))
add(EditEntity("chapterUrl", tr?.chapterUrl, R.string.rule_chapter_url))
@@ -402,12 +402,12 @@ class BookSourceEditActivity :
"canReName" -> bookInfoRule.canReName = it.value
"downloadUrls" -> bookInfoRule.downloadUrls =
viewModel.ruleComplete(it.value, bookInfoRule.init)
"reGetTocUrl" -> bookInfoRule.reGetTocUrlOnRefresh = it.value
}
}
tocEntities.forEach {
when (it.key) {
"chapterList" -> tocRule.chapterList = it.value ?: ""
"preUpdateJs" -> tocRule.preUpdateJs = it.value
"chapterList" -> tocRule.chapterList = it.value
"chapterName" -> tocRule.chapterName =
viewModel.ruleComplete(it.value, tocRule.chapterList)
"chapterUrl" -> tocRule.chapterUrl =
@@ -15,6 +15,7 @@ import io.legado.app.help.DefaultData
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
@@ -116,7 +117,11 @@ class MainViewModel(application: Application) : BaseViewModel(application) {
onUpTocBooks.add(book.bookUrl)
postEvent(EventBus.UP_BOOKSHELF, book.bookUrl)
execute(context = upTocPool) {
if (book.tocUrl.isBlank() || source.isReGetTocUrlOnRefresh()) {
val preUpdateJs = source.ruleToc?.preUpdateJs
if (!preUpdateJs.isNullOrBlank()) {
AnalyzeRule(book, source).evalJS(preUpdateJs)
}
if (book.tocUrl.isBlank()) {
WebBook.getBookInfoAwait(this, source, book)
}
val toc = WebBook.getChapterListAwait(this, source, book).getOrThrow()