This commit is contained in:
Horis
2024-07-31 17:42:19 +08:00
parent 1376feaecf
commit bc7b9383f5
18 changed files with 117 additions and 40 deletions
@@ -1,7 +1,7 @@
package io.legado.app.data.entities
import cn.hutool.crypto.symmetric.AES
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.constant.AppConst
import io.legado.app.constant.AppLog
@@ -231,16 +231,15 @@ interface BaseSource : JsExtensions {
* 执行JS
*/
@Throws(Exception::class)
fun evalJS(jsStr: String, bindingsConfig: SimpleBindings.() -> Unit = {}): Any? {
val bindings = SimpleBindings()
fun evalJS(jsStr: String, bindingsConfig: ScriptBindings.() -> Unit = {}): Any? {
val bindings = ScriptBindings()
bindings.apply(bindingsConfig)
bindings["java"] = this
bindings["source"] = this
bindings["baseUrl"] = getKey()
bindings["cookie"] = CookieStore
bindings["cache"] = CacheManager
val context = RhinoScriptEngine.getScriptContext(bindings)
val scope = RhinoScriptEngine.getRuntimeScope(context)
val scope = RhinoScriptEngine.getRuntimeScope(bindings)
getShareScope()?.let {
scope.prototype = it
}
@@ -3,7 +3,7 @@
package io.legado.app.help.book
import android.net.Uri
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.constant.AppLog
import io.legado.app.constant.BookSourceType
@@ -273,7 +273,7 @@ fun Book.getExportFileName(suffix: String): String {
if (jsStr.isNullOrBlank()) {
return "$name 作者:${getRealAuthor()}.$suffix"
}
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["epubIndex"] = ""// 兼容老版本,修复可能存在的错误
bindings["name"] = name
bindings["author"] = getRealAuthor()
@@ -297,7 +297,7 @@ fun Book.getExportFileName(
if (jsStr.isNullOrBlank()) {
return default
}
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["name"] = name
bindings["author"] = getRealAuthor()
bindings["epubIndex"] = epubIndex
@@ -327,7 +327,7 @@ fun Book.readSimulating(): Boolean {
}
fun tryParesExportFileName(jsStr: String): Boolean {
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["name"] = "name"
bindings["author"] = "author"
bindings["epubIndex"] = "epubIndex"
@@ -1,7 +1,7 @@
package io.legado.app.model
import com.google.gson.reflect.TypeToken
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.exception.NoStackTraceException
import io.legado.app.help.http.newCallStrResponse
@@ -33,7 +33,7 @@ object SharedJsScope {
var scope = scopeMap[key]?.get()
if (scope == null) {
scope = RhinoScriptEngine.run {
getRuntimeScope(getScriptContext(SimpleBindings()))
getRuntimeScope(ScriptBindings())
}
if (jsLib.isJsonObject()) {
val jsMap: Map<String, String> = GSON.fromJson(
@@ -2,7 +2,7 @@ package io.legado.app.model.analyzeRule
import android.text.TextUtils
import androidx.annotation.Keep
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.constant.AppPattern.JS_PATTERN
import io.legado.app.data.entities.BaseBook
@@ -21,7 +21,6 @@ import io.legado.app.utils.isJson
import io.legado.app.utils.printOnDebug
import io.legado.app.utils.splitNotBlank
import io.legado.app.utils.stackTraceStr
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
import org.apache.commons.text.StringEscapeUtils
@@ -746,7 +745,7 @@ class AnalyzeRule(
* 执行JS
*/
fun evalJS(jsStr: String, result: Any? = null): Any? {
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["java"] = this
bindings["cookie"] = CookieStore
bindings["cache"] = CacheManager
@@ -758,8 +757,7 @@ class AnalyzeRule(
bindings["title"] = chapter?.title
bindings["src"] = content
bindings["nextChapterUrl"] = nextChapterUrl
val context = RhinoScriptEngine.getScriptContext(bindings)
val scope = RhinoScriptEngine.getRuntimeScope(context)
val scope = RhinoScriptEngine.getRuntimeScope(bindings)
source?.getShareScope()?.let {
scope.prototype = it
}
@@ -6,7 +6,7 @@ import androidx.annotation.Keep
import androidx.media3.common.MediaItem
import cn.hutool.core.util.HexUtil
import com.bumptech.glide.load.model.GlideUrl
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.constant.AppConst.UA_NAME
import io.legado.app.constant.AppPattern
@@ -266,7 +266,7 @@ class AnalyzeUrl(
* 执行JS
*/
fun evalJS(jsStr: String, result: Any? = null): Any? {
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["java"] = this
bindings["baseUrl"] = baseUrl
bindings["cookie"] = CookieStore
@@ -278,8 +278,7 @@ class AnalyzeUrl(
bindings["book"] = ruleData as? Book
bindings["source"] = source
bindings["result"] = result
val context = RhinoScriptEngine.getScriptContext(bindings)
val scope = RhinoScriptEngine.getRuntimeScope(context)
val scope = RhinoScriptEngine.getRuntimeScope(bindings)
source?.getShareScope()?.let {
scope.prototype = it
}
@@ -3,7 +3,7 @@ package io.legado.app.model.localBook
import android.net.Uri
import android.util.Base64
import androidx.documentfile.provider.DocumentFile
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.R
import io.legado.app.constant.*
@@ -291,7 +291,7 @@ object LocalBook {
AppConfig.bookImportFileName + "\nJSON.stringify({author:author,name:name})"
//在脚本中定义如何分解文件名成书名、作者名
val jsonStr = RhinoScriptEngine.run {
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["src"] = tempFileName
eval(js, bindings)
}.toString()
@@ -1,7 +1,7 @@
package io.legado.app.model.webBook
import android.text.TextUtils
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.R
import io.legado.app.data.entities.Book
@@ -127,7 +127,7 @@ object BookChapterList {
Debug.log(book.origin, "◇目录总数:${list.size}")
coroutineContext.ensureActive()
val formatJs = tocRule.formatJs
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["gInt"] = 0
list.forEachIndexed { index, bookChapter ->
bookChapter.index = index
@@ -1,7 +1,7 @@
package io.legado.app.utils
import androidx.core.os.postDelayed
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.exception.RegexTimeoutException
import io.legado.app.help.CrashHandler
@@ -32,7 +32,7 @@ fun CharSequence.replace(regex: Regex, replacement: String, timeout: Long): Stri
while (matcher.find()) {
if (isJs) {
val jsResult = RhinoScriptEngine.run {
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["result"] = matcher.group()
eval(replacement1, bindings)
}.toString()