diff --git a/app/src/main/java/io/legado/app/help/http/CookieManager.kt b/app/src/main/java/io/legado/app/help/http/CookieManager.kt index fbd720ca3df8..cc666b9a942e 100644 --- a/app/src/main/java/io/legado/app/help/http/CookieManager.kt +++ b/app/src/main/java/io/legado/app/help/http/CookieManager.kt @@ -1,9 +1,11 @@ package io.legado.app.help.http +import android.webkit.CookieManager import io.legado.app.constant.AppLog import io.legado.app.data.appDb import io.legado.app.help.CacheManager import io.legado.app.utils.NetworkUtils +import io.legado.app.utils.splitNotBlank import okhttp3.Cookie import okhttp3.Headers import okhttp3.HttpUrl @@ -139,6 +141,14 @@ object CookieManager { } } + fun applyToWebView(url: String) { + val cookies = CookieStore.getCookie(url).splitNotBlank(";") + val cookieManager = CookieManager.getInstance() + cookies.forEach { + cookieManager.setCookie(url, it) + } + } + fun List.getString() = buildString { this@getString.forEachIndexed { index, cookie -> if (index > 0) append("; ") diff --git a/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt b/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt index e222718c987c..99191e185a8e 100644 --- a/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt +++ b/app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt @@ -47,6 +47,7 @@ import io.legado.app.utils.toggleNavigationBar import io.legado.app.utils.viewbindingdelegate.viewBinding import io.legado.app.utils.visible import java.net.URLDecoder +import io.legado.app.help.http.CookieManager as AppCookieManager class WebViewActivity : VMBaseActivity() { @@ -171,8 +172,7 @@ class WebViewActivity : VMBaseActivity() { userAgentString = it } } - val cookieManager = CookieManager.getInstance() - cookieManager.setCookie(url, CookieStore.getCookie(url)) + AppCookieManager.applyToWebView(url) binding.webView.addJavascriptInterface(this, "app") binding.webView.setOnLongClickListener { val hitTestResult = binding.webView.hitTestResult diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt index 6c8c5478c9ea..23796b9ba94a 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt @@ -10,7 +10,6 @@ import android.view.Menu import android.view.MenuItem import android.view.View import android.view.WindowManager -import android.webkit.CookieManager import android.webkit.JavascriptInterface import android.webkit.SslErrorHandler import android.webkit.URLUtil @@ -35,7 +34,7 @@ import io.legado.app.constant.AppLog import io.legado.app.data.entities.RssSource import io.legado.app.databinding.ActivityRssReadBinding import io.legado.app.help.config.AppConfig -import io.legado.app.help.http.CookieStore +import io.legado.app.help.http.CookieManager import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.selector import io.legado.app.lib.theme.accentColor @@ -308,8 +307,7 @@ class ReadRssActivity : VMBaseActivity } viewModel.urlLiveData.observe(this) { upJavaScriptEnable() - val cookieManager = CookieManager.getInstance() - cookieManager.setCookie(it.url, CookieStore.getCookie(it.url)) + CookieManager.applyToWebView(it.url) binding.webView.settings.userAgentString = it.getUserAgent() binding.webView.loadUrl(it.url, it.headerMap) }