|
|
@@ -1,6 +1,5 @@
|
|
|
package io.legado.app.help
|
|
|
|
|
|
-import android.net.Uri
|
|
|
import android.webkit.WebSettings
|
|
|
import androidx.annotation.Keep
|
|
|
import cn.hutool.core.codec.Base64
|
|
|
@@ -39,12 +38,8 @@ import io.legado.app.utils.createFileReplace
|
|
|
import io.legado.app.utils.externalCache
|
|
|
import io.legado.app.utils.fromJsonObject
|
|
|
import io.legado.app.utils.isAbsUrl
|
|
|
-import io.legado.app.utils.isContentScheme
|
|
|
-import io.legado.app.utils.isUri
|
|
|
import io.legado.app.utils.longToastOnUi
|
|
|
import io.legado.app.utils.mapAsync
|
|
|
-import io.legado.app.utils.readBytes
|
|
|
-import io.legado.app.utils.readText
|
|
|
import io.legado.app.utils.stackTraceStr
|
|
|
import io.legado.app.utils.startActivity
|
|
|
import io.legado.app.utils.toStringArray
|
|
|
@@ -104,14 +99,12 @@ interface JsExtensions : JsEncodeUtils {
|
|
|
url.toString()
|
|
|
}
|
|
|
val analyzeUrl = AnalyzeUrl(urlStr, source = getSource(), coroutineContext = context)
|
|
|
- return runBlocking(context) {
|
|
|
- kotlin.runCatching {
|
|
|
- analyzeUrl.getStrResponseAwait().body
|
|
|
- }.onFailure {
|
|
|
- AppLog.put("ajax(${urlStr}) error\n${it.localizedMessage}", it)
|
|
|
- }.getOrElse {
|
|
|
- it.stackTraceStr
|
|
|
- }
|
|
|
+ return kotlin.runCatching {
|
|
|
+ analyzeUrl.getStrResponse().body
|
|
|
+ }.onFailure {
|
|
|
+ AppLog.put("ajax(${urlStr}) error\n${it.localizedMessage}", it)
|
|
|
+ }.getOrElse {
|
|
|
+ it.stackTraceStr
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -121,7 +114,11 @@ interface JsExtensions : JsEncodeUtils {
|
|
|
fun ajaxAll(urlList: Array<String>): Array<StrResponse> {
|
|
|
return runBlocking(context) {
|
|
|
urlList.asFlow().mapAsync(AppConfig.threadCount) { url ->
|
|
|
- val analyzeUrl = AnalyzeUrl(url, source = getSource())
|
|
|
+ val analyzeUrl = AnalyzeUrl(
|
|
|
+ url,
|
|
|
+ source = getSource(),
|
|
|
+ coroutineContext = coroutineContext
|
|
|
+ )
|
|
|
analyzeUrl.getStrResponseAwait()
|
|
|
}.flowOn(IO).toList().toTypedArray()
|
|
|
}
|
|
|
@@ -131,29 +128,34 @@ interface JsExtensions : JsEncodeUtils {
|
|
|
* 访问网络,返回Response<String>
|
|
|
*/
|
|
|
fun connect(urlStr: String): StrResponse {
|
|
|
- return runBlocking(context) {
|
|
|
- val analyzeUrl = AnalyzeUrl(urlStr, source = getSource())
|
|
|
- kotlin.runCatching {
|
|
|
- analyzeUrl.getStrResponseAwait()
|
|
|
- }.onFailure {
|
|
|
- AppLog.put("connect(${urlStr}) error\n${it.localizedMessage}", it)
|
|
|
- }.getOrElse {
|
|
|
- StrResponse(analyzeUrl.url, it.stackTraceStr)
|
|
|
- }
|
|
|
+ val analyzeUrl = AnalyzeUrl(
|
|
|
+ urlStr,
|
|
|
+ source = getSource(),
|
|
|
+ coroutineContext = context
|
|
|
+ )
|
|
|
+ return kotlin.runCatching {
|
|
|
+ analyzeUrl.getStrResponse()
|
|
|
+ }.onFailure {
|
|
|
+ AppLog.put("connect(${urlStr}) error\n${it.localizedMessage}", it)
|
|
|
+ }.getOrElse {
|
|
|
+ StrResponse(analyzeUrl.url, it.stackTraceStr)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
fun connect(urlStr: String, header: String?): StrResponse {
|
|
|
- return runBlocking(context) {
|
|
|
- val headerMap = GSON.fromJsonObject<Map<String, String>>(header).getOrNull()
|
|
|
- val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap, source = getSource())
|
|
|
- kotlin.runCatching {
|
|
|
- analyzeUrl.getStrResponseAwait()
|
|
|
- }.onFailure {
|
|
|
- AppLog.put("ajax($urlStr,$header) error\n${it.localizedMessage}", it)
|
|
|
- }.getOrElse {
|
|
|
- StrResponse(analyzeUrl.url, it.stackTraceStr)
|
|
|
- }
|
|
|
+ val headerMap = GSON.fromJsonObject<Map<String, String>>(header).getOrNull()
|
|
|
+ val analyzeUrl = AnalyzeUrl(
|
|
|
+ urlStr,
|
|
|
+ headerMapF = headerMap,
|
|
|
+ source = getSource(),
|
|
|
+ coroutineContext = context
|
|
|
+ )
|
|
|
+ return kotlin.runCatching {
|
|
|
+ analyzeUrl.getStrResponse()
|
|
|
+ }.onFailure {
|
|
|
+ AppLog.put("ajax($urlStr,$header) error\n${it.localizedMessage}", it)
|
|
|
+ }.getOrElse {
|
|
|
+ StrResponse(analyzeUrl.url, it.stackTraceStr)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -247,13 +249,11 @@ interface JsExtensions : JsEncodeUtils {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 可从网络,本地文件(阅读私有缓存目录和书籍保存位置支持相对路径)导入JavaScript脚本
|
|
|
+ * 可从网络,本地文件(阅读私有数据目录相对路径)导入JavaScript脚本
|
|
|
*/
|
|
|
fun importScript(path: String): String {
|
|
|
val result = when {
|
|
|
path.startsWith("http") -> cacheFile(path)
|
|
|
- path.isUri() -> Uri.parse(path).readText(appCtx)
|
|
|
- path.startsWith("/storage") -> FileUtils.readText(path)
|
|
|
else -> readTxtFile(path)
|
|
|
}
|
|
|
if (result.isBlank()) throw NoStackTraceException("$path 内容获取失败或者为空")
|
|
|
@@ -561,7 +561,8 @@ interface JsExtensions : JsEncodeUtils {
|
|
|
cachePath + File.separator + path
|
|
|
}
|
|
|
val file = File(aPath)
|
|
|
- if (!file.canonicalPath.startsWith(cachePath)) {
|
|
|
+ val safePath = appCtx.externalCache.parent!!
|
|
|
+ if (!file.canonicalPath.startsWith(safePath)) {
|
|
|
throw SecurityException("非法路径")
|
|
|
}
|
|
|
return file
|
|
|
@@ -812,8 +813,6 @@ interface JsExtensions : JsEncodeUtils {
|
|
|
if (qTTF != null) return qTTF
|
|
|
}
|
|
|
val font: ByteArray? = when {
|
|
|
- data.isContentScheme() -> Uri.parse(data).readBytes(appCtx)
|
|
|
- data.startsWith("/storage") -> File(data).readBytes()
|
|
|
data.isAbsUrl() -> AnalyzeUrl(
|
|
|
data,
|
|
|
source = getSource(),
|