From 4bdc15c0a779d83b0d61eb0e9485ec9eb278ec8d Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 4 Jan 2022 15:24:18 +0800 Subject: [PATCH 1/3] rewrite func getCookie and add func cacheFile --- app/src/main/assets/updateLog.md | 8 +++++ .../java/io/legado/app/help/JsExtensions.kt | 34 ++++++++++++------- .../io/legado/app/help/http/CookieStore.kt | 6 ++++ 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index cf68c7d88..79c08a526 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -11,6 +11,14 @@ * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。 * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! +**2022/01/04** + +* 弃用java.getCookie(tag,key),请使用cookie.getKey(url,key) +* js添加java.cacheFile(url, saveTime),缓存网络链接,返回文件内容,可实现代码共用和减少代码量 +```js +eval(String(java.cacheFile(url))) +``` + **2022/01/03** * 重新安装应用后没有权限的本地书籍会提示选择文件所在文件夹,不需要重新添加 diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index 90ec06c55..b84ed8f7b 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -8,6 +8,7 @@ import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst.dateFormat import io.legado.app.data.entities.BaseSource import io.legado.app.help.http.* +import io.legado.app.help.CacheManager import io.legado.app.model.Debug import io.legado.app.model.analyzeRule.AnalyzeUrl import io.legado.app.model.analyzeRule.QueryTTF @@ -125,6 +126,26 @@ interface JsExtensions { } } + /** + * 缓存网络文件 + * @param url 网络文件的链接 + * @param saveTime 缓存时间,单位:秒 + * @return 返回缓存后的文件内容 + */ + fun cacheFile(url: String): String? { + return cacheFile(url, 0) + } + + fun cacheFile(url: String, saveTime: Int = 0): String? { + val key = md5Encode16(url) + val _cache = CacheManager.get(key) + val value = ajax(url) ?: "" + if(_cache.isNullOrBlank()) { + CacheManager.put(key, value as Any, saveTime) + } + return _cache + } + /** * 实现16进制字符串转文件 * @param content 需要转成文件的16进制字符串 @@ -174,19 +195,6 @@ interface JsExtensions { .execute() } - /** - *js实现读取cookie - */ - fun getCookie(tag: String, key: String? = null): String { - val cookie = CookieStore.getCookie(tag) - val cookieMap = CookieStore.cookieToMap(cookie) - return if (key != null) { - cookieMap[key] ?: "" - } else { - cookie - } - } - /** * js实现解码,不能删 */ diff --git a/app/src/main/java/io/legado/app/help/http/CookieStore.kt b/app/src/main/java/io/legado/app/help/http/CookieStore.kt index c8d516091..f9fba9520 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieStore.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieStore.kt @@ -34,6 +34,12 @@ object CookieStore : CookieManager { val cookieBean = appDb.cookieDao.get(NetworkUtils.getSubDomain(url)) return cookieBean?.cookie ?: "" } + + fun getKey(url: String, key: String): String { + val cookie = getCookie(url) + val cookieMap = cookieToMap(cookie) + return cookieMap[key] ?: "" + } override fun removeCookie(url: String) { appDb.cookieDao.delete(NetworkUtils.getSubDomain(url)) From 97bfb324e72bf80ffb15339371347d7caeef69be Mon Sep 17 00:00:00 2001 From: Xwite <1797350009@qq.com> Date: Tue, 4 Jan 2022 15:25:35 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E4=B9=A6=E6=BA=90?= =?UTF-8?q?=E8=B0=83=E8=AF=95=E6=98=BE=E7=A4=BA=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/res/layout/activity_source_debug.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/res/layout/activity_source_debug.xml b/app/src/main/res/layout/activity_source_debug.xml index 4061274ec..5405684ab 100644 --- a/app/src/main/res/layout/activity_source_debug.xml +++ b/app/src/main/res/layout/activity_source_debug.xml @@ -176,7 +176,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/secondaryText" - android:text="调试详情页>>输入详情页URL,如:" + android:text="调试正文页>>输入正文页URL,如:" tools:ignore="HardcodedText" /> Date: Tue, 4 Jan 2022 16:19:07 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E5=B8=AE=E5=8A=A9?= =?UTF-8?q?=E6=96=87=E6=A1=A3;=E6=9B=B4=E6=96=B0cacheFile?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/src/main/assets/help/ruleHelp.md | 17 +++++++++++++++-- .../java/io/legado/app/help/JsExtensions.kt | 5 +++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/assets/help/ruleHelp.md b/app/src/main/assets/help/ruleHelp.md index 043677f4b..b2b1bb0b8 100644 --- a/app/src/main/assets/help/ruleHelp.md +++ b/app/src/main/assets/help/ruleHelp.md @@ -31,8 +31,21 @@ * 获取登录后的cookie ``` -java.getCookie("http://baidu.com", null) => userid=1234;pwd=adbcd -java.getCookie("http://baidu.com", "userid") => 1234 +获取全部 +cookie.getCookie(url) +获取某一键值 +cookie.getKey(url,key) +``` + +* 缓存网络文件 +``` +获取 +java.cacheFile(url) +java.cacheFile(url,saveTime) +执行内容 +eval(String(java.cacheFile(url))) +删除缓存文件 +cache.delete(java.md5Encode16(url)) ``` * 请求头,支持http代理,socks4 socks5代理设置 diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index b84ed8f7b..d420a32de 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -139,8 +139,9 @@ interface JsExtensions { fun cacheFile(url: String, saveTime: Int = 0): String? { val key = md5Encode16(url) val _cache = CacheManager.get(key) - val value = ajax(url) ?: "" - if(_cache.isNullOrBlank()) { + if(_cache.isNullOrBlank()) { + log("首次下载${url}...") + val value = ajax(url) ?: "" CacheManager.put(key, value as Any, saveTime) } return _cache