Skip to content

Commit

Permalink
优化
Browse files Browse the repository at this point in the history
  • Loading branch information
821938089 committed Jan 17, 2025
1 parent ea6b7cb commit 6128027
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
10 changes: 10 additions & 0 deletions app/src/main/java/io/legado/app/help/http/CookieManager.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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<Cookie>.getString() = buildString {
this@getString.forEachIndexed { index, cookie ->
if (index > 0) append("; ")
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ActivityWebViewBinding, WebViewModel>() {

Expand Down Expand Up @@ -171,8 +172,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -308,8 +307,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
}
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)
}
Expand Down

0 comments on commit 6128027

Please sign in to comment.