Add clear cookies option to WebView menu

This commit is contained in:
arkon 2022-04-13 17:48:05 -04:00
parent 7b5106d206
commit 2a070c0b1e
4 changed files with 24 additions and 7 deletions

View file

@ -29,9 +29,9 @@ class AndroidCookieJar : CookieJar {
} }
} }
fun remove(url: HttpUrl, cookieNames: List<String>? = null, maxAge: Int = -1) { fun remove(url: HttpUrl, cookieNames: List<String>? = null, maxAge: Int = -1): Int {
val urlString = url.toString() val urlString = url.toString()
val cookies = manager.getCookie(urlString) ?: return val cookies = manager.getCookie(urlString) ?: return 0
fun List<String>.filterNames(): List<String> { fun List<String>.filterNames(): List<String> {
return if (cookieNames != null) { return if (cookieNames != null) {
@ -41,10 +41,11 @@ class AndroidCookieJar : CookieJar {
} }
} }
cookies.split(";") return cookies.split(";")
.map { it.substringBefore("=") } .map { it.substringBefore("=") }
.filterNames() .filterNames()
.onEach { manager.setCookie(urlString, "$it=;Max-Age=$maxAge") } .onEach { manager.setCookie(urlString, "$it=;Max-Age=$maxAge") }
.count()
} }
fun removeAll() { fun removeAll() {

View file

@ -259,11 +259,11 @@ class ExtensionDetailsController(bundle: Bundle? = null) :
?.map { it.baseUrl } ?.map { it.baseUrl }
?.distinct() ?: emptyList() ?.distinct() ?: emptyList()
urls.forEach { val cleared = urls.sumOf {
network.cookieManager.remove(it.toHttpUrl()) network.cookieManager.remove(it.toHttpUrl())
} }
logcat { "Cleared cookies for: ${urls.joinToString()}" } logcat { "Cleared $cleared cookies for: ${urls.joinToString()}" }
} }
private fun Source.isEnabled(): Boolean { private fun Source.isEnabled(): Boolean {

View file

@ -17,17 +17,20 @@ import androidx.lifecycle.lifecycleScope
import eu.kanade.tachiyomi.BuildConfig import eu.kanade.tachiyomi.BuildConfig
import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.R
import eu.kanade.tachiyomi.databinding.WebviewActivityBinding import eu.kanade.tachiyomi.databinding.WebviewActivityBinding
import eu.kanade.tachiyomi.network.NetworkHelper
import eu.kanade.tachiyomi.source.SourceManager import eu.kanade.tachiyomi.source.SourceManager
import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.source.online.HttpSource
import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.base.activity.BaseActivity
import eu.kanade.tachiyomi.util.system.WebViewClientCompat import eu.kanade.tachiyomi.util.system.WebViewClientCompat
import eu.kanade.tachiyomi.util.system.WebViewUtil import eu.kanade.tachiyomi.util.system.WebViewUtil
import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.getResourceColor
import eu.kanade.tachiyomi.util.system.logcat
import eu.kanade.tachiyomi.util.system.openInBrowser import eu.kanade.tachiyomi.util.system.openInBrowser
import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.system.setDefaultSettings
import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.system.toast
import kotlinx.coroutines.flow.launchIn import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach import kotlinx.coroutines.flow.onEach
import okhttp3.HttpUrl.Companion.toHttpUrl
import reactivecircus.flowbinding.appcompat.navigationClicks import reactivecircus.flowbinding.appcompat.navigationClicks
import reactivecircus.flowbinding.swiperefreshlayout.refreshes import reactivecircus.flowbinding.swiperefreshlayout.refreshes
import uy.kohesive.injekt.injectLazy import uy.kohesive.injekt.injectLazy
@ -37,11 +40,16 @@ class WebViewActivity : BaseActivity() {
private lateinit var binding: WebviewActivityBinding private lateinit var binding: WebviewActivityBinding
private val sourceManager: SourceManager by injectLazy() private val sourceManager: SourceManager by injectLazy()
private val network: NetworkHelper by injectLazy()
private var bundle: Bundle? = null private var bundle: Bundle? = null
private var isRefreshing: Boolean = false private var isRefreshing: Boolean = false
init {
registerSecureActivity(this)
}
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
@ -176,6 +184,7 @@ class WebViewActivity : BaseActivity() {
R.id.action_web_refresh -> refreshPage() R.id.action_web_refresh -> refreshPage()
R.id.action_web_share -> shareWebpage() R.id.action_web_share -> shareWebpage()
R.id.action_web_browser -> openInBrowser() R.id.action_web_browser -> openInBrowser()
R.id.action_clear_cookies -> clearCookies()
} }
return super.onOptionsItemSelected(item) return super.onOptionsItemSelected(item)
} }
@ -207,8 +216,10 @@ class WebViewActivity : BaseActivity() {
openInBrowser(binding.webview.url!!, forceDefaultBrowser = true) openInBrowser(binding.webview.url!!, forceDefaultBrowser = true)
} }
init { private fun clearCookies() {
registerSecureActivity(this) val url = binding.webview.url!!
val cleared = network.cookieManager.remove(url.toHttpUrl())
logcat { "Cleared $cleared cookies for: $url" }
} }
companion object { companion object {

View file

@ -31,4 +31,9 @@
android:title="@string/action_open_in_browser" android:title="@string/action_open_in_browser"
app:showAsAction="never" /> app:showAsAction="never" />
<item
android:id="@+id/action_clear_cookies"
android:title="@string/pref_clear_cookies"
app:showAsAction="never" />
</menu> </menu>