This commit is contained in:
kunfei
2023-04-17 10:26:36 +08:00
parent a27219c0a6
commit c3502fa6ea
2 changed files with 20 additions and 6 deletions
@@ -7,9 +7,9 @@ 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.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.isAbsUrl import io.legado.app.utils.isAbsUrl
import io.legado.app.utils.isJsonObject import io.legado.app.utils.isJsonObject
import org.mozilla.javascript.Context
import org.mozilla.javascript.Scriptable import org.mozilla.javascript.Scriptable
import java.lang.ref.WeakReference import java.lang.ref.WeakReference
import kotlin.collections.set import kotlin.collections.set
@@ -27,8 +27,7 @@ object SharedJsScope {
if (scope == null) { if (scope == null) {
val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings()) val context = SCRIPT_ENGINE.getScriptContext(SimpleBindings())
scope = SCRIPT_ENGINE.getRuntimeScope(context) scope = SCRIPT_ENGINE.getRuntimeScope(context)
val rhino = Context.enter() Rhino.use {
rhino.runCatching {
if (jsLib.isJsonObject()) { if (jsLib.isJsonObject()) {
val jsMap: Map<String, String> = GSON.fromJson( val jsMap: Map<String, String> = GSON.fromJson(
jsLib, jsLib,
@@ -43,14 +42,13 @@ object SharedJsScope {
val js = okHttpClient.newCallStrResponse { val js = okHttpClient.newCallStrResponse {
url(value) url(value)
}.body }.body
evaluateString(scope, js, "jsLib", 1, null) it.evaluateString(scope, js, "jsLib", 1, null)
} }
} }
} else { } else {
evaluateString(scope, jsLib, "jsLib", 1, null) it.evaluateString(scope, jsLib, "jsLib", 1, null)
} }
} }
Context.exit()
scopeMap[key] = WeakReference(scope) scopeMap[key] = WeakReference(scope)
} }
return scope return scope
@@ -0,0 +1,16 @@
package io.legado.app.utils
import org.mozilla.javascript.Context
object Rhino {
inline fun <R> use(block: (Context) -> R): R {
val context = Context.enter()
return try {
block.invoke(context)
} finally {
Context.exit()
}
}
}