优化
This commit is contained in:
@@ -12,6 +12,7 @@ import io.legado.app.help.JsExtensions
|
|||||||
import io.legado.app.help.config.AppConfig
|
import io.legado.app.help.config.AppConfig
|
||||||
import io.legado.app.help.crypto.SymmetricCryptoAndroid
|
import io.legado.app.help.crypto.SymmetricCryptoAndroid
|
||||||
import io.legado.app.help.http.CookieStore
|
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.help.source.getShareScope
|
||||||
import io.legado.app.utils.GSON
|
import io.legado.app.utils.GSON
|
||||||
import io.legado.app.utils.fromJsonArray
|
import io.legado.app.utils.fromJsonArray
|
||||||
@@ -234,9 +235,10 @@ interface BaseSource : JsExtensions {
|
|||||||
*/
|
*/
|
||||||
@Throws(Exception::class)
|
@Throws(Exception::class)
|
||||||
fun evalJS(jsStr: String, bindingsConfig: ScriptBindings.() -> Unit = {}): Any? {
|
fun evalJS(jsStr: String, bindingsConfig: ScriptBindings.() -> Unit = {}): Any? {
|
||||||
|
val sourceCopy = copy()
|
||||||
val bindings = buildScriptBindings { bindings ->
|
val bindings = buildScriptBindings { bindings ->
|
||||||
bindings["java"] = this
|
bindings["java"] = sourceCopy
|
||||||
bindings["source"] = this
|
bindings["source"] = sourceCopy
|
||||||
bindings["baseUrl"] = getKey()
|
bindings["baseUrl"] = getKey()
|
||||||
bindings["cookie"] = CookieStore
|
bindings["cookie"] = CookieStore
|
||||||
bindings["cache"] = CacheManager
|
bindings["cache"] = CacheManager
|
||||||
|
|||||||
@@ -30,6 +30,7 @@ import io.legado.app.utils.isJson
|
|||||||
import io.legado.app.utils.printOnDebug
|
import io.legado.app.utils.printOnDebug
|
||||||
import io.legado.app.utils.splitNotBlank
|
import io.legado.app.utils.splitNotBlank
|
||||||
import io.legado.app.utils.stackTraceStr
|
import io.legado.app.utils.stackTraceStr
|
||||||
|
import io.legado.app.utils.updateVariableTo
|
||||||
import kotlinx.coroutines.ensureActive
|
import kotlinx.coroutines.ensureActive
|
||||||
import kotlinx.coroutines.runBlocking
|
import kotlinx.coroutines.runBlocking
|
||||||
import kotlinx.coroutines.withTimeout
|
import kotlinx.coroutines.withTimeout
|
||||||
@@ -80,8 +81,6 @@ class AnalyzeRule(
|
|||||||
private var topScopeRef: WeakReference<Scriptable>? = null
|
private var topScopeRef: WeakReference<Scriptable>? = null
|
||||||
private var evalJSCallCount = 0
|
private var evalJSCallCount = 0
|
||||||
|
|
||||||
private val sourceCopy = source?.copy()
|
|
||||||
|
|
||||||
private var coroutineContext: CoroutineContext = EmptyCoroutineContext
|
private var coroutineContext: CoroutineContext = EmptyCoroutineContext
|
||||||
|
|
||||||
private var loggedNonStandardJSON = false
|
private var loggedNonStandardJSON = false
|
||||||
@@ -768,19 +767,21 @@ class AnalyzeRule(
|
|||||||
* 执行JS
|
* 执行JS
|
||||||
*/
|
*/
|
||||||
fun evalJS(jsStr: String, result: Any? = null): Any? {
|
fun evalJS(jsStr: String, result: Any? = null): Any? {
|
||||||
|
val chapterCopy = chapter?.copy()
|
||||||
|
val rssArticleCopy = rssArticle?.copy()
|
||||||
val bindings = buildScriptBindings { bindings ->
|
val bindings = buildScriptBindings { bindings ->
|
||||||
bindings["java"] = this
|
bindings["java"] = this
|
||||||
bindings["cookie"] = CookieStore
|
bindings["cookie"] = CookieStore
|
||||||
bindings["cache"] = CacheManager
|
bindings["cache"] = CacheManager
|
||||||
bindings["source"] = sourceCopy
|
bindings["source"] = source?.copy()
|
||||||
bindings["book"] = book
|
bindings["book"] = book
|
||||||
bindings["result"] = result
|
bindings["result"] = result
|
||||||
bindings["baseUrl"] = baseUrl
|
bindings["baseUrl"] = baseUrl
|
||||||
bindings["chapter"] = chapter
|
bindings["chapter"] = chapterCopy
|
||||||
bindings["title"] = chapter?.title
|
bindings["title"] = chapterCopy?.title
|
||||||
bindings["src"] = content
|
bindings["src"] = content
|
||||||
bindings["nextChapterUrl"] = nextChapterUrl
|
bindings["nextChapterUrl"] = nextChapterUrl
|
||||||
bindings["rssArticle"] = rssArticle
|
bindings["rssArticle"] = rssArticleCopy
|
||||||
}
|
}
|
||||||
val topScope = source?.getShareScope(coroutineContext) ?: topScopeRef?.get()
|
val topScope = source?.getShareScope(coroutineContext) ?: topScopeRef?.get()
|
||||||
val scope = if (topScope == null) {
|
val scope = if (topScope == null) {
|
||||||
@@ -795,7 +796,18 @@ class AnalyzeRule(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
val script = compileScriptCache(jsStr)
|
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 {
|
private fun compileScriptCache(jsStr: String): CompiledScript {
|
||||||
|
|||||||
@@ -38,6 +38,7 @@ import io.legado.app.help.http.newCallStrResponse
|
|||||||
import io.legado.app.help.http.postForm
|
import io.legado.app.help.http.postForm
|
||||||
import io.legado.app.help.http.postJson
|
import io.legado.app.help.http.postJson
|
||||||
import io.legado.app.help.http.postMultipart
|
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.help.source.getShareScope
|
||||||
import io.legado.app.model.Debug
|
import io.legado.app.model.Debug
|
||||||
import io.legado.app.utils.EncoderUtils
|
import io.legado.app.utils.EncoderUtils
|
||||||
@@ -357,7 +358,7 @@ class AnalyzeUrl(
|
|||||||
bindings["speakText"] = speakText
|
bindings["speakText"] = speakText
|
||||||
bindings["speakSpeed"] = speakSpeed
|
bindings["speakSpeed"] = speakSpeed
|
||||||
bindings["book"] = ruleData as? Book
|
bindings["book"] = ruleData as? Book
|
||||||
bindings["source"] = source
|
bindings["source"] = source?.copy()
|
||||||
bindings["result"] = result
|
bindings["result"] = result
|
||||||
}
|
}
|
||||||
val sharedScope = source?.getShareScope(coroutineContext)
|
val sharedScope = source?.getShareScope(coroutineContext)
|
||||||
|
|||||||
@@ -6,3 +6,10 @@ fun BookChapter.internString() {
|
|||||||
title = title.intern()
|
title = title.intern()
|
||||||
bookUrl = bookUrl.intern()
|
bookUrl = bookUrl.intern()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun BookChapter.updateVariableTo(chapter: BookChapter) {
|
||||||
|
if (variable != chapter.variable) {
|
||||||
|
chapter.variableMap.clear()
|
||||||
|
chapter.variableMap.putAll(variableMap)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user