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. 32
      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
@ -104,7 +104,7 @@ 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,6 +114,13 @@ interface JsExtensions {
return contents.toString() return contents.toString()
} }
/**
* js实现文件夹/文件的删除
*/
fun deleteFolder(path: String) {
FileUtils.deleteFile(path)
}
/** /**
* js实现重定向拦截,网络访问get * js实现重定向拦截,网络访问get
*/ */
@ -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))
}
/** /**
* 解析字体,返回字体解析类 * 解析字体,返回字体解析类
*/ */

Loading…
Cancel
Save