jsLib完成

This commit is contained in:
kunfei 2023-04-18 09:08:54 +08:00
parent 3498daa61b
commit f8181aa0db
13 changed files with 1896 additions and 41 deletions

File diff suppressed because it is too large Load Diff

View File

@ -21,7 +21,7 @@ val appDb by lazy {
}
@Database(
version = 65,
version = 66,
exportSchema = true,
entities = [Book::class, BookGroup::class, BookSource::class, BookChapter::class,
ReplaceRule::class, SearchBook::class, SearchKeyword::class, Cookie::class,
@ -50,7 +50,8 @@ val appDb by lazy {
AutoMigration(from = 61, to = 62),
AutoMigration(from = 62, to = 63),
AutoMigration(from = 63, to = 64),
AutoMigration(from = 64, to = 65, spec = DatabaseMigrations.Migration_64_65::class)
AutoMigration(from = 64, to = 65, spec = DatabaseMigrations.Migration_64_65::class),
AutoMigration(from = 65, to = 66)
]
)
abstract class AppDatabase : RoomDatabase() {

View File

@ -10,8 +10,10 @@ import io.legado.app.help.CacheManager
import io.legado.app.help.JsExtensions
import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.CookieStore
import io.legado.app.model.SharedJsScope
import io.legado.app.utils.*
import org.intellij.lang.annotations.Language
import org.mozilla.javascript.Scriptable
/**
* 可在js里调用,source.xxx()
@ -43,6 +45,11 @@ interface BaseSource : JsExtensions {
*/
var enabledCookieJar: Boolean?
/**
* js库
*/
var jsLib: String?
fun getTag(): String
fun getKey(): String
@ -230,4 +237,8 @@ interface BaseSource : JsExtensions {
bindings["cache"] = CacheManager
return SCRIPT_ENGINE.eval(jsStr, bindings)
}
fun getShareScope(): Scriptable? {
return SharedJsScope.getScope(jsLib)
}
}

View File

@ -41,7 +41,7 @@ data class BookSource(
@ColumnInfo(defaultValue = "1")
var enabledExplore: Boolean = true,
// js库
var jsLib: String? = null,
override var jsLib: String? = null,
// 启用okhttp CookieJAr 自动保存每次请求的cookie
@ColumnInfo(defaultValue = "0")
override var enabledCookieJar: Boolean? = true,

View File

@ -24,6 +24,7 @@ data class HttpTTS(
override var loginUrl: String? = null,
override var loginUi: String? = null,
override var header: String? = null,
override var jsLib: String? = null,
@ColumnInfo(defaultValue = "0")
override var enabledCookieJar: Boolean? = false,
var loginCheckJs: String? = null,

View File

@ -28,7 +28,7 @@ data class RssSource(
// 自定义变量说明
var variableComment: String? = null,
// js库
var jsLib: String? = null,
override var jsLib: String? = null,
// 启用okhttp CookieJAr 自动保存每次请求的cookie
@ColumnInfo(defaultValue = "0")
override var enabledCookieJar: Boolean? = true,

View File

@ -2,7 +2,6 @@ package io.legado.app.help.source
import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.ExploreKind
import io.legado.app.model.SharedJsScope
import io.legado.app.utils.ACache
import io.legado.app.utils.GSON
import io.legado.app.utils.MD5Utils
@ -13,16 +12,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext
import org.mozilla.javascript.Scriptable
import java.util.concurrent.ConcurrentHashMap
import kotlin.collections.List
import kotlin.collections.arrayListOf
import kotlin.collections.emptyList
import kotlin.collections.filterNotNull
import kotlin.collections.first
import kotlin.collections.forEach
import kotlin.collections.getOrNull
import kotlin.collections.hashMapOf
import kotlin.collections.set
/**
@ -93,10 +83,6 @@ suspend fun BookSource.clearExploreKindsCache() {
}
}
suspend fun BookSource.getShareScope(): Scriptable? {
return SharedJsScope.getScope(jsLib)
}
fun BookSource.contains(word: String?): Boolean {
if (word.isNullOrEmpty()) {
return true

View File

@ -1,13 +1,11 @@
package io.legado.app.help.source
import io.legado.app.data.entities.RssSource
import io.legado.app.model.SharedJsScope
import io.legado.app.utils.ACache
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import org.mozilla.javascript.Scriptable
private val aCache by lazy { ACache.get("rssSortUrl") }
@ -59,7 +57,3 @@ suspend fun RssSource.removeSortCache() {
aCache.remove(getSortUrlsKey())
}
}
suspend fun RssSource.getShareScope(): Scriptable? {
return SharedJsScope.getScope(jsLib)
}

View File

@ -155,6 +155,7 @@ object BookCover {
override var loginUrl: String? = null,
override var loginUi: String? = null,
override var header: String? = null,
override var jsLib: String? = null,
override var enabledCookieJar: Boolean? = false,
) : BaseSource {

View File

@ -12,6 +12,7 @@ import io.legado.app.utils.MD5Utils
import io.legado.app.utils.Rhino
import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonObject
import kotlinx.coroutines.runBlocking
import org.mozilla.javascript.Scriptable
import splitties.init.appCtx
import java.io.File
@ -25,7 +26,7 @@ object SharedJsScope {
private val scopeMap = hashMapOf<String, WeakReference<Scriptable>>()
suspend fun getScope(jsLib: String?): Scriptable? {
fun getScope(jsLib: String?): Scriptable? {
if (jsLib.isNullOrBlank()) {
return null
}
@ -49,9 +50,11 @@ object SharedJsScope {
val fileName = MD5Utils.md5Encode(value)
var js = aCache.getAsString(fileName)
if (js == null) {
js = okHttpClient.newCallStrResponse {
url(value)
}.body
js = runBlocking {
okHttpClient.newCallStrResponse {
url(value)
}.body
}
if (js !== null) {
aCache.put(fileName, js)
} else {

View File

@ -693,7 +693,12 @@ class AnalyzeRule(
bindings["title"] = chapter?.title
bindings["src"] = content
bindings["nextChapterUrl"] = nextChapterUrl
return SCRIPT_ENGINE.eval(jsStr, bindings)
val context = SCRIPT_ENGINE.getScriptContext(bindings)
val scope = SCRIPT_ENGINE.getRuntimeScope(context)
source?.getShareScope()?.let {
scope.prototype = it
}
return SCRIPT_ENGINE.eval(jsStr, scope)
}
override fun getSource(): BaseSource? {

View File

@ -265,7 +265,12 @@ class AnalyzeUrl(
bindings["book"] = ruleData as? Book
bindings["source"] = source
bindings["result"] = result
return SCRIPT_ENGINE.eval(jsStr, bindings)
val context = SCRIPT_ENGINE.getScriptContext(bindings)
val scope = SCRIPT_ENGINE.getRuntimeScope(context)
source?.getShareScope()?.let {
scope.prototype = it
}
return SCRIPT_ENGINE.eval(jsStr, scope)
}
fun put(key: String, value: String): String {
@ -475,7 +480,6 @@ class AnalyzeUrl(
}
try {
setCookie()
@Suppress("BlockingMethodInNonBlockingContext")
val response = getProxyClient(proxy).newCallResponse(retry) {
addHeaders(headerMap)
when (method) {
@ -514,7 +518,6 @@ class AnalyzeUrl(
private fun getByteArrayIfDataUri(): ByteArray? {
@Suppress("RegExpRedundantEscape")
val dataUriFindResult = dataUriRegex.find(urlNoQuery)
@Suppress("BlockingMethodInNonBlockingContext")
if (dataUriFindResult != null) {
val dataUriBase64 = dataUriFindResult.groupValues[1]
val byteArray = Base64.decode(dataUriBase64, Base64.DEFAULT)
@ -526,7 +529,6 @@ class AnalyzeUrl(
/**
* 访问网站,返回ByteArray
*/
@Suppress("UnnecessaryVariable", "LiftReturnOrAssignment")
@Throws(ConcurrentException::class)
suspend fun getByteArrayAwait(): ByteArray {
getByteArrayIfDataUri()?.let {
@ -544,7 +546,6 @@ class AnalyzeUrl(
/**
* 访问网站,返回InputStream
*/
@Suppress("LiftReturnOrAssignment")
@Throws(ConcurrentException::class)
suspend fun getInputStreamAwait(): InputStream {
getByteArrayIfDataUri()?.let {

View File

@ -330,13 +330,6 @@ object WebBook {
}
}
/**
* 获取段评
*/
fun getReview() {
// TODO
}
/**
* 精准搜索
*/