pull/2692/head
kunfei 2 years ago
parent 06352e3b3a
commit baa1ebb140
  1. 59
      app/src/main/java/io/legado/app/data/entities/BaseSource.kt

@ -10,18 +10,37 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.CookieStore import io.legado.app.help.http.CookieStore
import io.legado.app.utils.* import io.legado.app.utils.*
import org.intellij.lang.annotations.Language
/** /**
* 可在js里调用,source.xxx() * 可在js里调用,source.xxx()
*/ */
@Suppress("unused") @Suppress("unused")
interface BaseSource : JsExtensions { interface BaseSource : JsExtensions {
/**
* 并发率
*/
var concurrentRate: String?
/**
* 登录地址
*/
var loginUrl: String?
/**
* 登录UI
*/
var loginUi: String?
var concurrentRate: String? // 并发率 /**
var loginUrl: String? // 登录地址 * 请求头
var loginUi: String? // 登录UI */
var header: String? // 请求头 var header: String?
var enabledCookieJar: Boolean? //启用cookieJar
/**
* 启用cookieJar
*/
var enabledCookieJar: Boolean?
fun getTag(): String fun getTag(): String
@ -32,8 +51,7 @@ interface BaseSource : JsExtensions {
} }
fun loginUi(): List<RowUi>? { fun loginUi(): List<RowUi>? {
return GSON.fromJsonArray<RowUi>(loginUi) return GSON.fromJsonArray<RowUi>(loginUi).onFailure {
.onFailure {
it.printOnDebug() it.printOnDebug()
}.getOrNull() }.getOrNull()
} }
@ -43,16 +61,24 @@ interface BaseSource : JsExtensions {
return when { return when {
loginJs == null -> null loginJs == null -> null
loginJs.startsWith("@js:") -> loginJs.substring(4) loginJs.startsWith("@js:") -> loginJs.substring(4)
loginJs.startsWith("<js>") -> loginJs.startsWith("<js>") -> loginJs.substring(4, loginJs.lastIndexOf("<"))
loginJs.substring(4, loginJs.lastIndexOf("<"))
else -> loginJs else -> loginJs
} }
} }
// 调用login函数 实现登录请求 // 调用login函数 实现登录请求
fun login() { fun login() {
getLoginJs()?.let { val loginJs = getLoginJs()
evalJS("$it\nif(typeof login=='function'){login.apply(this);}else{throw('Function login not implements!!!')}") if (!loginJs.isNullOrBlank()) {
@Language("js")
val js = """$loginJs
if(typeof login=='function'){
login.apply(this);
} else {
throw('Function login not implements!!!')
}
""".trimIndent()
evalJS(js)
} }
} }
@ -63,10 +89,13 @@ interface BaseSource : JsExtensions {
header?.let { header?.let {
GSON.fromJsonObject<Map<String, String>>( GSON.fromJsonObject<Map<String, String>>(
when { when {
it.startsWith("@js:", true) -> it.startsWith("@js:", true) -> evalJS(it.substring(4)).toString()
evalJS(it.substring(4)).toString() it.startsWith("<js>", true) -> evalJS(
it.startsWith("<js>", true) -> it.substring(
evalJS(it.substring(4, it.lastIndexOf("<"))).toString() 4,
it.lastIndexOf("<")
)
).toString()
else -> it else -> it
} }
).getOrNull()?.let { map -> ).getOrNull()?.let { map ->

Loading…
Cancel
Save