diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 011689a1b..430408e94 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -6,7 +6,11 @@ **2020/10/23** * 修复选择错误的bug * 修复长图最后一张不能滚动的bug -* js添加java.getCookie(sourceUrl)来获取登录后的cookie by [AndyBernie](https://github.com/AndyBernie) +* js添加java.getCookie(sourceUrl:String, key:String? = null)来获取登录后的cookie by [AndyBernie](https://github.com/AndyBernie) +``` +java.getCookie("http://baidu.com", null) => userid=1234;pwd=adbcd +java.getCookie("http://baidu.com", "userid") => 1234 +``` * 修复简繁转换没有处理标题 **2020/10/21** 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 ff7357ad4..f42828476 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -123,8 +123,14 @@ interface JsExtensions { /** *js实现读取cookie */ - fun getCookie(tag: String): String { - return CookieStore.getCookie(tag) + fun getCookie(tag: String, key: String? = null): String { + val cookie = CookieStore.getCookie(tag) + val cookieMap = CookieStore.cookieToMap(cookie) + return if (key != null) { + cookieMap.get(key) ?: "" + } else { + cookie + } } /** 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 2f2bfaaec..a565560b7 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 @@ -43,7 +43,7 @@ object CookieStore : CookiePersistor { App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) } - private fun cookieToMap(cookie: String): MutableMap { + fun cookieToMap(cookie: String): MutableMap { val cookieMap = mutableMapOf() if (cookie.isBlank()) { return cookieMap @@ -63,7 +63,7 @@ object CookieStore : CookiePersistor { return cookieMap } - private fun mapToCookie(cookieMap: Map?): String? { + fun mapToCookie(cookieMap: Map?): String? { if (cookieMap == null || cookieMap.isEmpty()) { return null } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index 160c0debf..a7eb08968 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -281,7 +281,13 @@ class AnalyzeUrl( fun getResponse(tag: String): Call { val cookie = CookieStore.getCookie(tag) if (cookie.isNotEmpty()) { - headerMap["Cookie"] += ";${cookie}" + val cookieMap = CookieStore.cookieToMap(cookie) + val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "") + cookieMap.putAll(customCookieMap) + val newCookie = CookieStore.mapToCookie(cookieMap) + newCookie?.let { + headerMap.put("Cookie", it) + } } return when { method == RequestMethod.POST -> { @@ -312,6 +318,16 @@ class AnalyzeUrl( if (type != null) { return Res(url, StringUtils.byteToHexString(getResponseBytes(tag))) } + val cookie = CookieStore.getCookie(tag) + if (cookie.isNotEmpty()) { + val cookieMap = CookieStore.cookieToMap(cookie) + val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "") + cookieMap.putAll(customCookieMap) + val newCookie = CookieStore.mapToCookie(cookieMap) + newCookie?.let { + headerMap.put("Cookie", it) + } + } if (useWebView) { val params = AjaxWebView.AjaxParams(url) params.headerMap = headerMap @@ -322,10 +338,6 @@ class AnalyzeUrl( params.tag = tag return HttpHelper.ajax(params) } - val cookie = CookieStore.getCookie(tag) - if (cookie.isNotEmpty()) { - headerMap["Cookie"] += ";${cookie}" - } val res = when { method == RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { @@ -356,7 +368,13 @@ class AnalyzeUrl( if (tag != null) { val cookie = CookieStore.getCookie(tag) if (cookie.isNotEmpty()) { - headerMap["Cookie"] += ";${cookie}" + val cookieMap = CookieStore.cookieToMap(cookie) + val customCookieMap = CookieStore.cookieToMap(headerMap.get("Cookie") ?: "") + cookieMap.putAll(customCookieMap) + val newCookie = CookieStore.mapToCookie(cookieMap) + newCookie?.let { + headerMap.put("Cookie", it) + } } } val response = when {