This commit is contained in:
Horis
2024-07-31 17:42:19 +08:00
parent beb561a052
commit 7e6a1e4749
18 changed files with 117 additions and 40 deletions
@@ -1,7 +1,7 @@
package io.legado.app
import cn.hutool.core.lang.JarClassLoader
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import dalvik.system.DexClassLoader
import org.intellij.lang.annotations.Language
@@ -69,7 +69,7 @@ class AndroidJsTest {
@Test
fun testMap() {
val map = hashMapOf("id" to "3242532321")
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["result"] = map
@Language("js")
val jsMap = "$=result;id=$.id;id"
@@ -21,7 +21,7 @@ class MigrationTest {
)
@Rule
@get:Rule
val helper: MigrationTestHelper = MigrationTestHelper(
InstrumentationRegistry.getInstrumentation(),
AppDatabase::class.java.canonicalName,
@@ -32,7 +32,7 @@ class MigrationTest {
@Throws(IOException::class)
fun migrateAll() {
// Create earliest version of the database.
helper.createDatabase(TEST_DB, 30).apply {
helper.createDatabase(TEST_DB, 50).apply {
close()
}
@@ -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()
+18 -6
View File
@@ -1,6 +1,6 @@
package io.legado.app
import com.script.SimpleBindings
import com.script.ScriptBindings
import com.script.rhino.RhinoScriptEngine
import io.legado.app.data.entities.BookChapter
import org.intellij.lang.annotations.Language
@@ -28,7 +28,7 @@ class JsTest {
@Test
fun testMap() {
val map = hashMapOf("id" to "3242532321")
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["result"] = map
@Language("js")
val jsMap = "$=result;id=$.id;id"
@@ -43,7 +43,7 @@ class JsTest {
@Test
fun testFor() {
val scope = RhinoScriptEngine.run {
val scope = getRuntimeScope(getScriptContext(SimpleBindings()))
val scope = getRuntimeScope(ScriptBindings())
eval(printJs, scope)
scope
}
@@ -86,7 +86,7 @@ class JsTest {
s[2].substr(0,n);
""".trimIndent()
val x = RhinoScriptEngine.run {
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["result"] = "筳彩涫第七百一十四章 人头树鮺舦綸"
eval(js, bindings)
}
@@ -97,7 +97,7 @@ class JsTest {
@Test
fun chapterText() {
val chapter = BookChapter(title = "xxxyyy")
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["chapter"] = chapter
@Language("js")
val js = "chapter.title"
@@ -108,7 +108,7 @@ class JsTest {
@Test
fun javaListForEach() {
val list = arrayListOf(1, 2, 3)
val bindings = SimpleBindings()
val bindings = ScriptBindings()
bindings["list"] = list
@Language("js")
val js = """
@@ -120,4 +120,16 @@ class JsTest {
Assert.assertEquals(result, 6.0)
}
@Test
fun typeofString() {
val bindings = ScriptBindings()
@Language("js")
val js = """
s = "" + String()
typeof s
""".trimIndent()
val result = RhinoScriptEngine.eval(js, bindings)
Assert.assertEquals(result, "string")
}
}