This commit is contained in:
Horis
2025-03-27 15:26:50 +08:00
parent 4b23cc04f3
commit 9feaa3a1ea
5 changed files with 20 additions and 13 deletions
@@ -12,14 +12,13 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.SymmetricCryptoAndroid import io.legado.app.help.SymmetricCryptoAndroid
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.model.SharedJsScope import io.legado.app.help.source.getShareScope
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray import io.legado.app.utils.fromJsonArray
import io.legado.app.utils.fromJsonObject import io.legado.app.utils.fromJsonObject
import io.legado.app.utils.has import io.legado.app.utils.has
import io.legado.app.utils.printOnDebug import io.legado.app.utils.printOnDebug
import org.intellij.lang.annotations.Language import org.intellij.lang.annotations.Language
import org.mozilla.javascript.Scriptable
/** /**
* 可在js里调用,source.xxx() * 可在js里调用,source.xxx()
@@ -249,8 +248,4 @@ interface BaseSource : JsExtensions {
} }
return RhinoScriptEngine.eval(jsStr, scope) return RhinoScriptEngine.eval(jsStr, scope)
} }
fun getShareScope(): Scriptable? {
return SharedJsScope.getScope(jsLib)
}
} }
@@ -0,0 +1,10 @@
package io.legado.app.help.source
import io.legado.app.data.entities.BaseSource
import io.legado.app.model.SharedJsScope
import org.mozilla.javascript.Scriptable
import kotlin.coroutines.CoroutineContext
fun BaseSource.getShareScope(coroutineContext: CoroutineContext? = null): Scriptable? {
return SharedJsScope.getScope(jsLib, coroutineContext)
}
@@ -16,7 +16,7 @@ import org.mozilla.javascript.Scriptable
import splitties.init.appCtx import splitties.init.appCtx
import java.io.File import java.io.File
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import kotlin.collections.set import kotlin.coroutines.CoroutineContext
object SharedJsScope { object SharedJsScope {
@@ -25,7 +25,7 @@ object SharedJsScope {
private val scopeMap = hashMapOf<String, WeakReference<Scriptable>>() private val scopeMap = hashMapOf<String, WeakReference<Scriptable>>()
fun getScope(jsLib: String?): Scriptable? { fun getScope(jsLib: String?, coroutineContext: CoroutineContext?): Scriptable? {
if (jsLib.isNullOrBlank()) { if (jsLib.isNullOrBlank()) {
return null return null
} }
@@ -60,11 +60,11 @@ object SharedJsScope {
throw NoStackTraceException("下载jsLib-${value}失败") throw NoStackTraceException("下载jsLib-${value}失败")
} }
} }
RhinoScriptEngine.eval(js, scope) RhinoScriptEngine.eval(js, scope, coroutineContext)
} }
} }
} else { } else {
RhinoScriptEngine.eval(jsLib, scope) RhinoScriptEngine.eval(jsLib, scope, coroutineContext)
} }
scopeMap[key] = WeakReference(scope) scopeMap[key] = WeakReference(scope)
} }
@@ -14,6 +14,7 @@ import io.legado.app.data.entities.RssArticle
import io.legado.app.help.CacheManager import io.legado.app.help.CacheManager
import io.legado.app.help.JsExtensions import io.legado.app.help.JsExtensions
import io.legado.app.help.http.CookieStore import io.legado.app.help.http.CookieStore
import io.legado.app.help.source.getShareScope
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
@@ -748,7 +749,7 @@ class AnalyzeRule(
bindings["rssArticle"] = rssArticle bindings["rssArticle"] = rssArticle
} }
val scope = RhinoScriptEngine.getRuntimeScope(bindings) val scope = RhinoScriptEngine.getRuntimeScope(bindings)
source?.getShareScope()?.let { source?.getShareScope(coroutineContext)?.let {
scope.prototype = it scope.prototype = it
} }
return RhinoScriptEngine.eval(jsStr, scope, coroutineContext) return RhinoScriptEngine.eval(jsStr, scope, coroutineContext)
@@ -8,6 +8,7 @@ import cn.hutool.core.util.HexUtil
import com.bumptech.glide.load.model.GlideUrl import com.bumptech.glide.load.model.GlideUrl
import com.script.buildScriptBindings import com.script.buildScriptBindings
import com.script.rhino.RhinoScriptEngine import com.script.rhino.RhinoScriptEngine
import com.script.rhino.runScriptWithContext
import io.legado.app.constant.AppConst.UA_NAME import io.legado.app.constant.AppConst.UA_NAME
import io.legado.app.constant.AppPattern import io.legado.app.constant.AppPattern
import io.legado.app.constant.AppPattern.JS_PATTERN import io.legado.app.constant.AppPattern.JS_PATTERN
@@ -35,6 +36,7 @@ import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.postForm import io.legado.app.help.http.postForm
import io.legado.app.help.http.postJson import io.legado.app.help.http.postJson
import io.legado.app.help.http.postMultipart import io.legado.app.help.http.postMultipart
import io.legado.app.help.source.getShareScope
import io.legado.app.utils.EncoderUtils import io.legado.app.utils.EncoderUtils
import io.legado.app.utils.GSON import io.legado.app.utils.GSON
import io.legado.app.utils.NetworkUtils import io.legado.app.utils.NetworkUtils
@@ -45,7 +47,6 @@ import io.legado.app.utils.isJson
import io.legado.app.utils.isJsonArray import io.legado.app.utils.isJsonArray
import io.legado.app.utils.isJsonObject import io.legado.app.utils.isJsonObject
import io.legado.app.utils.isXml import io.legado.app.utils.isXml
import com.script.rhino.runScriptWithContext
import io.legado.app.utils.splitNotBlank import io.legado.app.utils.splitNotBlank
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import okhttp3.MediaType.Companion.toMediaType import okhttp3.MediaType.Companion.toMediaType
@@ -307,7 +308,7 @@ class AnalyzeUrl(
bindings["result"] = result bindings["result"] = result
} }
val scope = RhinoScriptEngine.getRuntimeScope(bindings) val scope = RhinoScriptEngine.getRuntimeScope(bindings)
source?.getShareScope()?.let { source?.getShareScope(coroutineContext)?.let {
scope.prototype = it scope.prototype = it
} }
return RhinoScriptEngine.eval(jsStr, scope, coroutineContext) return RhinoScriptEngine.eval(jsStr, scope, coroutineContext)