From f7170d9c2cdf4593ab58995468040a7ea0ca0c30 Mon Sep 17 00:00:00 2001 From: gedoor Date: Mon, 12 Jul 2021 15:19:18 +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 --- .../io/legado/app/data/entities/BookSource.kt | 93 +++++++++---------- 1 file changed, 46 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/io/legado/app/data/entities/BookSource.kt b/app/src/main/java/io/legado/app/data/entities/BookSource.kt index b4dac9758..c4b26ef09 100644 --- a/app/src/main/java/io/legado/app/data/entities/BookSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/BookSource.kt @@ -46,6 +46,52 @@ data class BookSource( var ruleContent: ContentRule? = null // 正文页规则 ) : Parcelable, JsExtensions { + @delegate:Transient + @delegate:Ignore + @IgnoredOnParcel + val exploreKinds by lazy { + val exploreUrl = exploreUrl?.trim() ?: return@lazy emptyList() + val kinds = arrayListOf() + var ruleStr = exploreUrl + if (ruleStr.isNotBlank()) { + kotlin.runCatching { + if (exploreUrl.startsWith("", false) + || exploreUrl.startsWith("@js", false) + ) { + val aCache = ACache.get(appCtx, "explore") + ruleStr = aCache.getAsString(bookSourceUrl) ?: "" + if (ruleStr.isBlank()) { + val bindings = SimpleBindings() + bindings["baseUrl"] = bookSourceUrl + bindings["java"] = this + bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager + val jsStr = if (exploreUrl.startsWith("@")) { + exploreUrl.substring(3) + } else { + exploreUrl.substring(4, exploreUrl.lastIndexOf("<")) + } + ruleStr = AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString().trim() + aCache.put(bookSourceUrl, ruleStr) + } + } + if (ruleStr.isJsonArray()) { + GSON.fromJsonArray(ruleStr)?.let { + kinds.addAll(it) + } + } else { + ruleStr.split("(&&|\n)+".toRegex()).forEach { kindStr -> + val kindCfg = kindStr.split("::") + kinds.add(ExploreKind(kindCfg.first(), kindCfg.getOrNull(1))) + } + } + }.onFailure { + kinds.add(ExploreKind(it.localizedMessage ?: "")) + } + } + return@lazy kinds + } + override fun hashCode(): Int { return bookSourceUrl.hashCode() } @@ -98,53 +144,6 @@ data class BookSource( } } - @delegate:Transient - @delegate:Ignore - @IgnoredOnParcel - val exploreKinds by lazy { - val exploreUrl = exploreUrl ?: return@lazy emptyList() - val kinds = arrayListOf() - var ruleStr = exploreUrl - if (ruleStr.isNotBlank()) { - kotlin.runCatching { - if (exploreUrl.startsWith("", false) - || exploreUrl.startsWith("@js", false) - ) { - val aCache = ACache.get(appCtx, "explore") - ruleStr = aCache.getAsString(bookSourceUrl) ?: "" - if (ruleStr.isBlank()) { - val bindings = SimpleBindings() - bindings["baseUrl"] = bookSourceUrl - bindings["java"] = this - bindings["cookie"] = CookieStore - bindings["cache"] = CacheManager - val jsStr = if (exploreUrl.startsWith("@")) { - exploreUrl.substring(3) - } else { - exploreUrl.substring(4, exploreUrl.lastIndexOf("<")) - } - ruleStr = AppConst.SCRIPT_ENGINE.eval(jsStr, bindings).toString().trim() - aCache.put(bookSourceUrl, ruleStr) - } - } - if (ruleStr.isJsonArray()) { - GSON.fromJsonArray(ruleStr)?.let { - kinds.addAll(it) - } - } else { - ruleStr.split("(&&|\n)+".toRegex()).forEach { c -> - val d = c.split("::") - if (d.size > 1) - kinds.add(ExploreKind(d[0], d[1])) - } - } - }.onFailure { - kinds.add(ExploreKind(it.localizedMessage ?: "")) - } - } - return@lazy kinds - } - /** * 执行JS */