pull/1259/head
gedoor 3 years ago
parent 05ab654cc8
commit 813456e875
  1. 45
      app/src/main/java/io/legado/app/data/entities/BaseSource.kt
  2. 35
      app/src/main/java/io/legado/app/data/entities/BookSource.kt
  3. 39
      app/src/main/java/io/legado/app/data/entities/RssSource.kt
  4. 4
      app/src/main/java/io/legado/app/data/entities/rule/LoginRule.kt
  5. 6
      app/src/main/java/io/legado/app/help/JsExtensions.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<String, String>().apply {
this[AppConst.UA_NAME] = AppConfig.userAgent
header?.let {
GSON.fromJsonObject<Map<String, String>>(
when {
it.startsWith("@js:", true) ->
evalJS(it.substring(4)).toString()
it.startsWith("<js>", 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)
}
}

@ -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<String, String>().apply {
this[AppConst.UA_NAME] = AppConfig.userAgent
header?.let {
GSON.fromJsonObject<Map<String, String>>(
when {
it.startsWith("@js:", true) ->
evalJS(it.substring(4)).toString()
it.startsWith("<js>", true) ->
evalJS(it.substring(4, it.lastIndexOf("<"))).toString()
else -> it
}
)?.let { map ->
putAll(map)
}
}
}) as Map<String, String>
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)

@ -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<String, String>().apply {
this[AppConst.UA_NAME] = AppConfig.userAgent
header?.let {
GSON.fromJsonObject<Map<String, String>>(
when {
it.startsWith("@js:", true) ->
evalJS(it.substring(4)).toString()
it.startsWith("<js>", 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)

@ -6,8 +6,8 @@ import kotlinx.parcelize.Parcelize
@Parcelize
data class LoginRule(
var ui: List<RowUi>? = 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

@ -71,15 +71,15 @@ interface JsExtensions {
/**
* 访问网络,返回Response<String>
*/
fun connect(urlStr: String): Any {
fun connect(urlStr: String): StrResponse {
return runBlocking {
kotlin.runCatching {
val analyzeUrl = AnalyzeUrl(urlStr)
kotlin.runCatching {
analyzeUrl.getStrResponse(urlStr)
}.onFailure {
it.printStackTrace()
}.getOrElse {
it.msg
StrResponse(analyzeUrl.url, it.localizedMessage)
}
}
}

Loading…
Cancel
Save