源添加jsLib,未完成

This commit is contained in:
kunfei 2023-04-10 20:54:52 +08:00
parent d23d12044a
commit 5006433437

View File

@ -1,5 +1,8 @@
package io.legado.app.model
import com.script.SimpleBindings
import io.legado.app.constant.SCRIPT_ENGINE
import io.legado.app.help.rhino.RhinoScriptEngine
import io.legado.app.utils.MD5Utils
import org.mozilla.javascript.Scriptable
import java.lang.ref.WeakReference
@ -8,12 +11,18 @@ object SharedJsScope {
private val scopeMap = hashMapOf<String, WeakReference<Scriptable>>()
fun getScope(jsLib: String) {
fun getScope(jsLib: String): Scriptable {
val key = MD5Utils.md5Encode(jsLib)
var scope = scopeMap[key]?.get()
if (scope == null) {
val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings())
scope = SCRIPT_ENGINE.getRuntimeScope(context)
RhinoScriptEngine.run {
it.evaluateString(scope, jsLib, "jsLib", 1, null)
}
scopeMap[key] = WeakReference(scope)
}
return scope
}
}