Horis 1 год назад
Родитель
Сommit
a6b7893a56

+ 4 - 2
app/src/main/java/io/legado/app/data/entities/BaseSource.kt

@@ -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

+ 19 - 7
app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt

@@ -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 {

+ 2 - 1
app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@@ -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)

+ 7 - 0
app/src/main/java/io/legado/app/utils/BookChapterExtensions.kt

@@ -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)
+    }
+}

+ 10 - 0
app/src/main/java/io/legado/app/utils/RssArticleExtensions.kt

@@ -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)
+    }
+}