From 813456e875211a4b58112f8735c5c08be9e1be88 Mon Sep 17 00:00:00 2001 From: gedoor Date: Sat, 21 Aug 2021 11:36:44 +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/BaseSource.kt | 45 +++++++++++++++++++ .../io/legado/app/data/entities/BookSource.kt | 35 +-------------- .../io/legado/app/data/entities/RssSource.kt | 39 +--------------- .../app/data/entities/rule/LoginRule.kt | 4 +- .../java/io/legado/app/help/JsExtensions.kt | 6 +-- 5 files changed, 54 insertions(+), 75 deletions(-) create mode 100644 app/src/main/java/io/legado/app/data/entities/BaseSource.kt diff --git a/app/src/main/java/io/legado/app/data/entities/BaseSource.kt b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt new file mode 100644 index 000000000..0c0929777 --- /dev/null +++ b/app/src/main/java/io/legado/app/data/entities/BaseSource.kt @@ -0,0 +1,45 @@ +package io.legado.app.data.entities + +import io.legado.app.constant.AppConst +import io.legado.app.help.AppConfig +import io.legado.app.help.CacheManager +import io.legado.app.help.JsExtensions +import io.legado.app.help.http.CookieStore +import io.legado.app.utils.GSON +import io.legado.app.utils.fromJsonObject +import java.util.* +import javax.script.SimpleBindings + +interface BaseSource : JsExtensions { + + var header: String? + + fun getHeaderMap() = HashMap().apply { + this[AppConst.UA_NAME] = AppConfig.userAgent + header?.let { + GSON.fromJsonObject>( + when { + it.startsWith("@js:", true) -> + evalJS(it.substring(4)).toString() + it.startsWith("", true) -> + evalJS(it.substring(4, it.lastIndexOf("<"))).toString() + else -> it + } + )?.let { map -> + putAll(map) + } + } + } + + /** + * 执行JS + */ + @Throws(Exception::class) + private fun evalJS(jsStr: String): Any? { + val bindings = SimpleBindings() + bindings["java"] = this + bindings["cookie"] = CookieStore + bindings["cache"] = CacheManager + return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) + } +} \ No newline at end of file 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 e706320e4..bfeb90f26 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 @@ -6,9 +6,7 @@ import androidx.room.* import io.legado.app.constant.AppConst import io.legado.app.constant.BookType import io.legado.app.data.entities.rule.* -import io.legado.app.help.AppConfig import io.legado.app.help.CacheManager -import io.legado.app.help.JsExtensions import io.legado.app.help.http.CookieStore import io.legado.app.utils.* import kotlinx.parcelize.IgnoredOnParcel @@ -33,7 +31,7 @@ data class BookSource( var customOrder: Int = 0, // 手动排序编号 var enabled: Boolean = true, // 是否启用 var enabledExplore: Boolean = true, // 启用发现 - var header: String? = null, // 请求头 + override var header: String? = null, // 请求头 var loginUrl: LoginRule? = null, // 登录地址 var bookSourceComment: String? = null, // 注释 var lastUpdateTime: Long = 0, // 最后更新时间,用于排序 @@ -45,7 +43,7 @@ data class BookSource( var ruleBookInfo: BookInfoRule? = null, // 书籍信息页规则 var ruleToc: TocRule? = null, // 目录页规则 var ruleContent: ContentRule? = null // 正文页规则 -) : Parcelable, JsExtensions { +) : Parcelable, BaseSource { @delegate:Transient @delegate:Ignore @@ -100,23 +98,6 @@ data class BookSource( override fun equals(other: Any?) = if (other is BookSource) other.bookSourceUrl == bookSourceUrl else false - @Throws(Exception::class) - fun getHeaderMap() = (HashMap().apply { - this[AppConst.UA_NAME] = AppConfig.userAgent - header?.let { - GSON.fromJsonObject>( - when { - it.startsWith("@js:", true) -> - evalJS(it.substring(4)).toString() - it.startsWith("", true) -> - evalJS(it.substring(4, it.lastIndexOf("<"))).toString() - else -> it - } - )?.let { map -> - putAll(map) - } - } - }) as Map fun getSearchRule() = ruleSearch ?: SearchRule() @@ -145,18 +126,6 @@ data class BookSource( } } - /** - * 执行JS - */ - @Throws(Exception::class) - private fun evalJS(jsStr: String): Any { - val bindings = SimpleBindings() - bindings["java"] = this - bindings["cookie"] = CookieStore - bindings["cache"] = CacheManager - return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) - } - fun equal(source: BookSource) = equal(bookSourceName, source.bookSourceName) && equal(bookSourceUrl, source.bookSourceUrl) diff --git a/app/src/main/java/io/legado/app/data/entities/RssSource.kt b/app/src/main/java/io/legado/app/data/entities/RssSource.kt index 81a4b4d99..49653a1ce 100644 --- a/app/src/main/java/io/legado/app/data/entities/RssSource.kt +++ b/app/src/main/java/io/legado/app/data/entities/RssSource.kt @@ -5,16 +5,11 @@ import androidx.room.Entity import androidx.room.Index import androidx.room.PrimaryKey import io.legado.app.constant.AppConst -import io.legado.app.help.AppConfig import io.legado.app.help.CacheManager -import io.legado.app.help.JsExtensions import io.legado.app.help.http.CookieStore import io.legado.app.utils.ACache -import io.legado.app.utils.GSON -import io.legado.app.utils.fromJsonObject import kotlinx.parcelize.Parcelize import splitties.init.appCtx -import java.util.* import javax.script.SimpleBindings @Parcelize @@ -41,12 +36,12 @@ data class RssSource( var ruleLink: String? = null, var ruleContent: String? = null, var style: String? = null, - var header: String? = null, + override var header: String? = null, var enableJs: Boolean = false, var loadWithBaseUrl: Boolean = false, var customOrder: Int = 0 -) : Parcelable, JsExtensions { +) : Parcelable, BaseSource { override fun equals(other: Any?): Boolean { if (other is RssSource) { @@ -57,36 +52,6 @@ data class RssSource( override fun hashCode() = sourceUrl.hashCode() - @Throws(Exception::class) - fun getHeaderMap() = HashMap().apply { - this[AppConst.UA_NAME] = AppConfig.userAgent - header?.let { - GSON.fromJsonObject>( - when { - it.startsWith("@js:", true) -> - evalJS(it.substring(4)).toString() - it.startsWith("", true) -> - evalJS(it.substring(4, it.lastIndexOf("<"))).toString() - else -> it - } - )?.let { map -> - putAll(map) - } - } - } - - /** - * 执行JS - */ - @Throws(Exception::class) - private fun evalJS(jsStr: String): Any? { - val bindings = SimpleBindings() - bindings["java"] = this - bindings["cookie"] = CookieStore - bindings["cache"] = CacheManager - return AppConst.SCRIPT_ENGINE.eval(jsStr, bindings) - } - fun equal(source: RssSource): Boolean { return equal(sourceUrl, source.sourceUrl) && equal(sourceIcon, source.sourceIcon) diff --git a/app/src/main/java/io/legado/app/data/entities/rule/LoginRule.kt b/app/src/main/java/io/legado/app/data/entities/rule/LoginRule.kt index a14ea5d67..d0391b9f8 100644 --- a/app/src/main/java/io/legado/app/data/entities/rule/LoginRule.kt +++ b/app/src/main/java/io/legado/app/data/entities/rule/LoginRule.kt @@ -6,8 +6,8 @@ import kotlinx.parcelize.Parcelize @Parcelize data class LoginRule( var ui: List? = null, - var url: String? = null, - var checkJs: String? = null + var url: String? = null, //url或js,js必须使用js标记 + var checkJs: String? = null, //只能写js,不用js标记 ) : Parcelable { @Parcelize diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index ab4eb815e..ee5efb4b8 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -71,15 +71,15 @@ interface JsExtensions { /** * 访问网络,返回Response */ - fun connect(urlStr: String): Any { + fun connect(urlStr: String): StrResponse { return runBlocking { + val analyzeUrl = AnalyzeUrl(urlStr) kotlin.runCatching { - val analyzeUrl = AnalyzeUrl(urlStr) analyzeUrl.getStrResponse(urlStr) }.onFailure { it.printStackTrace() }.getOrElse { - it.msg + StrResponse(analyzeUrl.url, it.localizedMessage) } } }