源添加jsLib,未完成

This commit is contained in:
kunfei
2023-04-17 19:41:20 +08:00
parent 4f723a6c28
commit 727e7f8582
3 changed files with 46 additions and 4 deletions
@@ -2,12 +2,28 @@ package io.legado.app.help.source
import io.legado.app.data.entities.BookSource import io.legado.app.data.entities.BookSource
import io.legado.app.data.entities.rule.ExploreKind import io.legado.app.data.entities.rule.ExploreKind
import io.legado.app.utils.* import io.legado.app.model.SharedJsScope
import io.legado.app.utils.ACache
import io.legado.app.utils.GSON
import io.legado.app.utils.MD5Utils
import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.isJsonArray
import io.legado.app.utils.printOnDebug
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.sync.Mutex import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock import kotlinx.coroutines.sync.withLock
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.mozilla.javascript.Scriptable
import java.util.concurrent.ConcurrentHashMap 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
/** /**
* 采用md5作为key可以在分类修改后自动重新计算,不需要手动刷新 * 采用md5作为key可以在分类修改后自动重新计算,不需要手动刷新
@@ -77,6 +93,10 @@ suspend fun BookSource.clearExploreKindsCache() {
} }
} }
suspend fun BookSource.getShareScope(): Scriptable? {
return SharedJsScope.getScope(jsLib)
}
fun BookSource.contains(word: String?): Boolean { fun BookSource.contains(word: String?): Boolean {
if (word.isNullOrEmpty()) { if (word.isNullOrEmpty()) {
return true return true
@@ -1,11 +1,13 @@
package io.legado.app.help.source package io.legado.app.help.source
import io.legado.app.data.entities.RssSource import io.legado.app.data.entities.RssSource
import io.legado.app.model.SharedJsScope
import io.legado.app.utils.ACache import io.legado.app.utils.ACache
import io.legado.app.utils.MD5Utils import io.legado.app.utils.MD5Utils
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import org.mozilla.javascript.Scriptable
private val aCache by lazy { ACache.get("rssSortUrl") } private val aCache by lazy { ACache.get("rssSortUrl") }
@@ -56,4 +58,8 @@ suspend fun RssSource.removeSortCache() {
withContext(Dispatchers.IO) { withContext(Dispatchers.IO) {
aCache.remove(getSortUrlsKey()) aCache.remove(getSortUrlsKey())
} }
}
suspend fun RssSource.getShareScope(): Scriptable? {
return SharedJsScope.getScope(jsLib)
} }
@@ -3,19 +3,26 @@ package io.legado.app.model
import com.google.gson.reflect.TypeToken import com.google.gson.reflect.TypeToken
import com.script.SimpleBindings import com.script.SimpleBindings
import io.legado.app.constant.SCRIPT_ENGINE import io.legado.app.constant.SCRIPT_ENGINE
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.http.newCallStrResponse import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.utils.ACache
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.MD5Utils import io.legado.app.utils.MD5Utils
import io.legado.app.utils.Rhino import io.legado.app.utils.Rhino
import io.legado.app.utils.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonObject import io.legado.app.utils.isJsonObject
import org.mozilla.javascript.Scriptable import org.mozilla.javascript.Scriptable
import splitties.init.appCtx
import java.io.File
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import kotlin.collections.set import kotlin.collections.set
object SharedJsScope { object SharedJsScope {
private val cacheFolder = File(appCtx.filesDir, "shareJs")
private val aCache = ACache.get(cacheFolder)
private val scopeMap = hashMapOf<String, WeakReference<Scriptable>>() private val scopeMap = hashMapOf<String, WeakReference<Scriptable>>()
suspend fun getScope(jsLib: String?): Scriptable? { suspend fun getScope(jsLib: String?): Scriptable? {
@@ -39,9 +46,18 @@ object SharedJsScope {
) )
jsMap.values.forEach { value -> jsMap.values.forEach { value ->
if (value.isAbsUrl()) { if (value.isAbsUrl()) {
val js = okHttpClient.newCallStrResponse { val fileName = MD5Utils.md5Encode(value)
url(value) var js = aCache.getAsString(fileName)
}.body if (js == null) {
js = okHttpClient.newCallStrResponse {
url(value)
}.body
if (js !== null) {
aCache.put(fileName, js)
} else {
throw NoStackTraceException("下载jsLib-${value}失败")
}
}
evaluateString(scope, js, "jsLib", 1, null) evaluateString(scope, js, "jsLib", 1, null)
} }
} }