优化
Some checks failed
Test Build / prepare (push) Has been cancelled
Test Build / build (app, release) (push) Has been cancelled
Test Build / build (app, releaseA) (push) Has been cancelled
Test Build / prerelease (push) Has been cancelled
Test Build / lanzou (push) Has been cancelled
Test Build / test_Branch (push) Has been cancelled
Test Build / telegram (push) Has been cancelled

This commit is contained in:
Horis
2025-05-16 09:03:51 +08:00
parent b9207b1802
commit e97c37765c
5 changed files with 42 additions and 10 deletions

View File

@@ -12,6 +12,7 @@ import io.legado.app.help.JsExtensions
import io.legado.app.help.config.AppConfig
import io.legado.app.help.crypto.SymmetricCryptoAndroid
import io.legado.app.help.http.CookieStore
import io.legado.app.help.source.copy
import io.legado.app.help.source.getShareScope
import io.legado.app.utils.GSON
import io.legado.app.utils.fromJsonArray
@@ -234,9 +235,10 @@ interface BaseSource : JsExtensions {
*/
@Throws(Exception::class)
fun evalJS(jsStr: String, bindingsConfig: ScriptBindings.() -> Unit = {}): Any? {
val sourceCopy = copy()
val bindings = buildScriptBindings { bindings ->
bindings["java"] = this
bindings["source"] = this
bindings["java"] = sourceCopy
bindings["source"] = sourceCopy
bindings["baseUrl"] = getKey()
bindings["cookie"] = CookieStore
bindings["cache"] = CacheManager

View File

@@ -30,6 +30,7 @@ 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 io.legado.app.utils.updateVariableTo
import kotlinx.coroutines.ensureActive
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withTimeout
@@ -80,8 +81,6 @@ class AnalyzeRule(
private var topScopeRef: WeakReference<Scriptable>? = null
private var evalJSCallCount = 0
private val sourceCopy = source?.copy()
private var coroutineContext: CoroutineContext = EmptyCoroutineContext
private var loggedNonStandardJSON = false
@@ -768,19 +767,21 @@ class AnalyzeRule(
* 执行JS
*/
fun evalJS(jsStr: String, result: Any? = null): Any? {
val chapterCopy = chapter?.copy()
val rssArticleCopy = rssArticle?.copy()
val bindings = buildScriptBindings { bindings ->
bindings["java"] = this
bindings["cookie"] = CookieStore
bindings["cache"] = CacheManager
bindings["source"] = sourceCopy
bindings["source"] = source?.copy()
bindings["book"] = book
bindings["result"] = result
bindings["baseUrl"] = baseUrl
bindings["chapter"] = chapter
bindings["title"] = chapter?.title
bindings["chapter"] = chapterCopy
bindings["title"] = chapterCopy?.title
bindings["src"] = content
bindings["nextChapterUrl"] = nextChapterUrl
bindings["rssArticle"] = rssArticle
bindings["rssArticle"] = rssArticleCopy
}
val topScope = source?.getShareScope(coroutineContext) ?: topScopeRef?.get()
val scope = if (topScope == null) {
@@ -795,7 +796,18 @@ class AnalyzeRule(
}
}
val script = compileScriptCache(jsStr)
return script.eval(scope, coroutineContext)
val result = script.eval(scope, coroutineContext)
updateVariable(chapterCopy, rssArticleCopy)
return result
}
private fun updateVariable(chapterCopy: BookChapter?, rssArticleCopy: RssArticle?) {
chapter?.let {
chapterCopy?.updateVariableTo(it)
}
rssArticle?.let {
rssArticleCopy?.updateVariableTo(it)
}
}
private fun compileScriptCache(jsStr: String): CompiledScript {

View File

@@ -38,6 +38,7 @@ import io.legado.app.help.http.newCallStrResponse
import io.legado.app.help.http.postForm
import io.legado.app.help.http.postJson
import io.legado.app.help.http.postMultipart
import io.legado.app.help.source.copy
import io.legado.app.help.source.getShareScope
import io.legado.app.model.Debug
import io.legado.app.utils.EncoderUtils
@@ -357,7 +358,7 @@ class AnalyzeUrl(
bindings["speakText"] = speakText
bindings["speakSpeed"] = speakSpeed
bindings["book"] = ruleData as? Book
bindings["source"] = source
bindings["source"] = source?.copy()
bindings["result"] = result
}
val sharedScope = source?.getShareScope(coroutineContext)

View File

@@ -6,3 +6,10 @@ fun BookChapter.internString() {
title = title.intern()
bookUrl = bookUrl.intern()
}
fun BookChapter.updateVariableTo(chapter: BookChapter) {
if (variable != chapter.variable) {
chapter.variableMap.clear()
chapter.variableMap.putAll(variableMap)
}
}

View File

@@ -0,0 +1,10 @@
package io.legado.app.utils
import io.legado.app.data.entities.RssArticle
fun RssArticle.updateVariableTo(rssArticle: RssArticle) {
if (variable != rssArticle.variable) {
rssArticle.variableMap.clear()
rssArticle.variableMap.putAll(variableMap)
}
}