feat:js添加importScript
This commit is contained in:
@@ -59,6 +59,12 @@ java.ajaxAll(urlList: Array<String>): Array<StrResponse?>
|
||||
java.log(msg)
|
||||
java.logType(var)
|
||||
```
|
||||
* 从网络(由java.cacheFile实现)、本地导入JavaScript脚本
|
||||
```
|
||||
{{java.importScript(url)}}
|
||||
{{java.importScript(relativePath)}}
|
||||
{{java.importScript(absolutePath)}}
|
||||
```
|
||||
* 缓存网络文件
|
||||
```
|
||||
获取
|
||||
|
||||
@@ -10,6 +10,8 @@ import io.legado.app.constant.AppConst
|
||||
import io.legado.app.constant.AppConst.dateFormat
|
||||
import io.legado.app.constant.AppLog
|
||||
import io.legado.app.data.entities.BaseSource
|
||||
import io.legado.app.exception.NoStackTraceException
|
||||
import io.legado.app.help.config.AppConfig
|
||||
import io.legado.app.help.http.*
|
||||
import io.legado.app.model.Debug
|
||||
import io.legado.app.model.analyzeRule.AnalyzeUrl
|
||||
@@ -127,6 +129,36 @@ interface JsExtensions {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 可从网络,本地文件(阅读私有缓存目录和书籍保存位置支持相对路径)导入JavaScript脚本
|
||||
*/
|
||||
fun importScript(path: String): String {
|
||||
var result: String = ""
|
||||
when {
|
||||
path.startsWith("http") -> result = cacheFile(path) ?: ""
|
||||
path.isContentScheme() -> result = DocumentUtils.readText(appCtx, Uri.parse(path))
|
||||
path.startsWith("/storage") -> result = FileUtils.readText(path)
|
||||
else -> {
|
||||
//先找书籍保存目录下有没有
|
||||
val publicStoragePath = AppConfig.defaultBookTreeUri
|
||||
val jsString = publicStoragePath?.let {
|
||||
val jsPath = if (path.startsWith("/")) path else "/" + path
|
||||
val filePathString = it + jsPath
|
||||
val fileUri = Uri.parse(filePathString)
|
||||
if (fileUri.isContentScheme()) {
|
||||
DocumentUtils.readText(appCtx, fileUri)
|
||||
} else {
|
||||
FileUtils.readText(fileUri.path!!)
|
||||
}
|
||||
}
|
||||
//私有目录
|
||||
result = if (jsString.isNullOrBlank()) readTxtFile(path) else jsString
|
||||
}
|
||||
}
|
||||
if (result.isBlank()) throw NoStackTraceException("${path} 内容获取失败或者为空")
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* 缓存以文本方式保存的文件 如.js .txt等
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user