Merge pull request #823 from Celeter/master

增加几个JS方法
pull/831/head
kunfei 4 years ago committed by GitHub
commit 461f92fc12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 74
      app/src/main/java/io/legado/app/help/JsExtensions.kt

@ -18,7 +18,7 @@ import rxhttp.wrapper.param.RxHttp
import rxhttp.wrapper.param.toByteArray import rxhttp.wrapper.param.toByteArray
import java.io.File import java.io.File
import java.net.URLEncoder import java.net.URLEncoder
import java.nio.charset.Charset import java.text.DateFormat
import java.util.* import java.util.*
@Keep @Keep
@ -63,8 +63,8 @@ interface JsExtensions {
fun downloadFile(content: String, url: String): String { fun downloadFile(content: String, url: String): String {
val type = AnalyzeUrl(url).type ?: return "" val type = AnalyzeUrl(url).type ?: return ""
val zipPath = FileUtils.getPath( val zipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
"${MD5Utils.md5Encode16(url)}.${type}" "${MD5Utils.md5Encode16(url)}.${type}"
) )
FileUtils.deleteFile(zipPath) FileUtils.deleteFile(zipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath)
@ -82,8 +82,8 @@ interface JsExtensions {
fun unzipFile(zipPath: String): String { fun unzipFile(zipPath: String): String {
if (zipPath.isEmpty()) return "" if (zipPath.isEmpty()) return ""
val unzipPath = FileUtils.getPath( val unzipPath = FileUtils.getPath(
FileUtils.createFolderIfNotExist(FileUtils.getCachePath()), FileUtils.createFolderIfNotExist(FileUtils.getCachePath()),
FileUtils.getNameExcludeExtension(zipPath) FileUtils.getNameExcludeExtension(zipPath)
) )
FileUtils.deleteFile(unzipPath) FileUtils.deleteFile(unzipPath)
val zipFile = FileUtils.createFileIfNotExist(zipPath) val zipFile = FileUtils.createFileIfNotExist(zipPath)
@ -104,8 +104,8 @@ interface JsExtensions {
if (it != null) { if (it != null) {
for (f in it) { for (f in it) {
val charsetName = EncodingDetect.getEncode(f) val charsetName = EncodingDetect.getEncode(f)
contents.append(String(f.readBytes(), Charset.forName(charsetName))) contents.append(String(f.readBytes(), charset(charsetName)))
.append("\n") .append("\n")
} }
contents.deleteCharAt(contents.length - 1) contents.deleteCharAt(contents.length - 1)
} }
@ -114,17 +114,24 @@ interface JsExtensions {
return contents.toString() return contents.toString()
} }
/**
* js实现文件夹/文件的删除
*/
fun deleteFolder(path: String) {
FileUtils.deleteFile(path)
}
/** /**
* js实现重定向拦截,网络访问get * js实现重定向拦截,网络访问get
*/ */
fun get(urlStr: String, headers: Map<String, String>): Connection.Response { fun get(urlStr: String, headers: Map<String, String>): Connection.Response {
return Jsoup.connect(urlStr) return Jsoup.connect(urlStr)
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory)
.ignoreContentType(true) .ignoreContentType(true)
.followRedirects(false) .followRedirects(false)
.headers(headers) .headers(headers)
.method(Connection.Method.GET) .method(Connection.Method.GET)
.execute() .execute()
} }
/** /**
@ -132,13 +139,13 @@ interface JsExtensions {
*/ */
fun post(urlStr: String, body: String, headers: Map<String, String>): Connection.Response { fun post(urlStr: String, body: String, headers: Map<String, String>): Connection.Response {
return Jsoup.connect(urlStr) return Jsoup.connect(urlStr)
.sslSocketFactory(SSLHelper.unsafeSSLSocketFactory) .sslSocketFactory(SSLHelper.unsafeSSLSocketFactory)
.ignoreContentType(true) .ignoreContentType(true)
.followRedirects(false) .followRedirects(false)
.requestBody(body) .requestBody(body)
.headers(headers) .headers(headers)
.method(Connection.Method.POST) .method(Connection.Method.POST)
.execute() .execute()
} }
/** /**
@ -202,6 +209,15 @@ interface JsExtensions {
return dateFormat.format(Date(time)) return dateFormat.format(Date(time))
} }
fun timeFormat(time: String): String {
val date = DateFormat.getDateTimeInstance().parse(time)
return if (date == null) {
""
} else {
dateFormat.format(date)
}
}
/** /**
* utf8编码转gbk编码 * utf8编码转gbk编码
*/ */
@ -234,10 +250,20 @@ interface JsExtensions {
/** /**
* 读取本地文件 * 读取本地文件
*/ */
fun readFile(path: String): ByteArray? { fun readFile(path: String): ByteArray {
return File(path).readBytes() return File(path).readBytes()
} }
fun readTxtFile(path: String): String {
val f = File(path)
val charsetName = EncodingDetect.getEncode(f)
return String(f.readBytes(), charset(charsetName))
}
fun readTxtFile(path: String, charsetName: String): String {
return String(File(path).readBytes(), charset(charsetName))
}
/** /**
* 解析字体,返回字体解析类 * 解析字体,返回字体解析类
*/ */
@ -275,9 +301,9 @@ interface JsExtensions {
} }
fun replaceFont( fun replaceFont(
text: String, text: String,
font1: QueryTTF?, font1: QueryTTF?,
font2: QueryTTF? font2: QueryTTF?
): String { ): String {
if (font1 == null || font2 == null) return text if (font1 == null || font2 == null) return text
val contentArray = text.toCharArray() val contentArray = text.toCharArray()

Loading…
Cancel
Save