diff --git a/app/src/main/assets/updateLog.md b/app/src/main/assets/updateLog.md index 7c40afcbc..8df9d933c 100644 --- a/app/src/main/assets/updateLog.md +++ b/app/src/main/assets/updateLog.md @@ -6,6 +6,7 @@ **2020/11/18** * 优化导航栏 * js添加java.log(msg: String)用于调试时输出消息 +* js添加cookie对象,可用方法见io.legado.app.help.http.api.CookieManager **2020/11/16** * 修复导出添加替换净化引起的bug diff --git a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt index db6dc165b..ddf64c895 100644 --- a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt +++ b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt @@ -117,10 +117,10 @@ class AjaxWebView { val isSniff: Boolean get() = !TextUtils.isEmpty(sourceRegex) - fun setCookie(url: String) { + fun putCookie(url: String) { tag?.let { val cookie = CookieManager.getInstance().getCookie(url) - CookieStore.setCookie(it, cookie) + CookieStore.putCookie(it, cookie) } } @@ -140,7 +140,7 @@ class AjaxWebView { ) : WebViewClient() { override fun onPageFinished(view: WebView, url: String) { - params.setCookie(url) + params.putCookie(url) val runnable = EvalJsRunnable(view, url, params.getJs(), handler) handler.postDelayed(runnable, 1000) } @@ -193,7 +193,7 @@ class AjaxWebView { } override fun onPageFinished(view: WebView, url: String) { - params.setCookie(url) + params.putCookie(url) if (params.hasJavaScript()) { evaluateJavascript(view, params.javaScript) params.clearJavaScript() 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 a565560b7..71eff8cb5 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 @@ -8,42 +8,43 @@ import com.franmontiel.persistentcookiejar.persistence.SerializableCookie import io.legado.app.App import io.legado.app.data.entities.Cookie import io.legado.app.help.coroutine.Coroutine +import io.legado.app.help.http.api.CookieManager import io.legado.app.utils.NetworkUtils -object CookieStore : CookiePersistor { +object CookieStore : CookiePersistor, CookieManager { - fun setCookie(url: String, cookie: String?) { + override fun putCookie(url: String, cookie: String?) { Coroutine.async { val cookieBean = Cookie(NetworkUtils.getSubDomain(url), cookie ?: "") App.db.cookieDao().insert(cookieBean) } } - fun replaceCookie(url: String, cookie: String) { + override fun replaceCookie(url: String, cookie: String) { if (TextUtils.isEmpty(url) || TextUtils.isEmpty(cookie)) { return } val oldCookie = getCookie(url) if (TextUtils.isEmpty(oldCookie)) { - setCookie(url, cookie) + putCookie(url, cookie) } else { val cookieMap = cookieToMap(oldCookie) cookieMap.putAll(cookieToMap(cookie)) val newCookie = mapToCookie(cookieMap) - setCookie(url, newCookie) + putCookie(url, newCookie) } } - fun getCookie(url: String): String { + override fun getCookie(url: String): String { val cookieBean = App.db.cookieDao().get(NetworkUtils.getSubDomain(url)) return cookieBean?.cookie ?: "" } - fun removeCookie(url: String) { + override fun removeCookie(url: String) { App.db.cookieDao().delete(NetworkUtils.getSubDomain(url)) } - fun cookieToMap(cookie: String): MutableMap { + override fun cookieToMap(cookie: String): MutableMap { val cookieMap = mutableMapOf() if (cookie.isBlank()) { return cookieMap @@ -63,7 +64,7 @@ object CookieStore : CookiePersistor { return cookieMap } - fun mapToCookie(cookieMap: Map?): String? { + override fun mapToCookie(cookieMap: Map?): String? { if (cookieMap == null || cookieMap.isEmpty()) { return null } diff --git a/app/src/main/java/io/legado/app/help/http/api/CookieManager.kt b/app/src/main/java/io/legado/app/help/http/api/CookieManager.kt new file mode 100644 index 000000000..66978d46b --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/api/CookieManager.kt @@ -0,0 +1,28 @@ +package io.legado.app.help.http.api + +interface CookieManager { + + /** + * 保存cookie + */ + fun putCookie(url: String, cookie: String?) + + /** + * 替换cookie + */ + fun replaceCookie(url: String, cookie: String) + + /** + * 获取cookie + */ + fun getCookie(url: String): String + + /** + * 移除cookie + */ + fun removeCookie(url: String) + + fun cookieToMap(cookie: String): MutableMap + + fun mapToCookie(cookieMap: Map?): String? +} \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt b/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt index dffe03695..3812040ca 100644 --- a/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt +++ b/app/src/main/java/io/legado/app/ui/login/SourceLogin.kt @@ -39,7 +39,7 @@ class SourceLogin : BaseActivity(R.layout.activity_source_login) { override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) { val cookie = cookieManager.getCookie(url) sourceUrl?.let { - CookieStore.setCookie(it, cookie) + CookieStore.putCookie(it, cookie) } super.onPageStarted(view, url, favicon) } @@ -47,7 +47,7 @@ class SourceLogin : BaseActivity(R.layout.activity_source_login) { override fun onPageFinished(view: WebView?, url: String?) { val cookie = cookieManager.getCookie(url) sourceUrl?.let { - CookieStore.setCookie(it, cookie) + CookieStore.putCookie(it, cookie) } if (checking) { finish()