fix: webview loading crash

This commit is contained in:
rebelonion 2024-03-18 17:27:32 -05:00
parent 39d6f0fbd6
commit 6a8e422a30
4 changed files with 17 additions and 11 deletions

View file

@ -11,7 +11,7 @@ class CloudFlare(override val location: FileUrl) : WebViewBottomDialog() {
override var title = "Cloudflare Bypass"
override val webViewClient = object : WebViewClient() {
override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
val cookie = cookies.getCookie(url.toString())
val cookie = cookies?.getCookie(url.toString())
if (cookie?.contains(cfTag) == true) {
val clearance = cookie.substringAfter("$cfTag=").substringBefore(";")
privateCallback.invoke(mapOf(cfTag to clearance))

View file

@ -34,8 +34,8 @@ class CookieCatcher : AppCompatActivity() {
val webView = findViewById<WebView>(R.id.discordWebview)
val cookies: CookieManager = Injekt.get<NetworkHelper>().cookieJar.manager
cookies.setAcceptThirdPartyCookies(webView, true)
val cookies: CookieManager? = Injekt.get<NetworkHelper>().cookieJar.manager
cookies?.setAcceptThirdPartyCookies(webView, true)
webView.apply {
settings.javaScriptEnabled = true

View file

@ -33,7 +33,7 @@ abstract class WebViewBottomDialog : BottomSheetDialogFragment() {
dismiss()
}
val cookies: CookieManager = Injekt.get<NetworkHelper>().cookieJar.manager
val cookies: CookieManager? = Injekt.get<NetworkHelper>().cookieJar.manager
//CookieManager.getInstance()
override fun onCreateView(
@ -52,7 +52,7 @@ abstract class WebViewBottomDialog : BottomSheetDialogFragment() {
javaScriptEnabled = true
userAgentString = defaultHeaders["User-Agent"]
}
cookies.setAcceptThirdPartyCookies(binding.webView, true)
cookies?.setAcceptThirdPartyCookies(binding.webView, true)
binding.webView.webViewClient = webViewClient
binding.webView.loadUrl(location.url, location.headers)
this.dismiss()

View file

@ -1,18 +1,24 @@
package eu.kanade.tachiyomi.network
import android.webkit.CookieManager
import ani.dantotsu.snackString
import okhttp3.Cookie
import okhttp3.CookieJar
import okhttp3.HttpUrl
class AndroidCookieJar : CookieJar {
val manager: CookieManager = CookieManager.getInstance()
val manager: CookieManager? = try {
CookieManager.getInstance()
} catch (e: Exception) {
snackString("Webview is outdated, please update your webview")
null
}
override fun saveFromResponse(url: HttpUrl, cookies: List<Cookie>) {
val urlString = url.toString()
cookies.forEach { manager.setCookie(urlString, it.toString()) }
cookies.forEach { manager?.setCookie(urlString, it.toString()) }
}
override fun loadForRequest(url: HttpUrl): List<Cookie> {
@ -20,9 +26,9 @@ class AndroidCookieJar : CookieJar {
}
fun get(url: HttpUrl): List<Cookie> {
val cookies = manager.getCookie(url.toString())
val cookies = manager?.getCookie(url.toString())
return if (cookies != null && cookies.isNotEmpty()) {
return if (!cookies.isNullOrEmpty()) {
cookies.split(";").mapNotNull { Cookie.parse(url, it) }
} else {
emptyList()
@ -31,7 +37,7 @@ class AndroidCookieJar : CookieJar {
fun remove(url: HttpUrl, cookieNames: List<String>? = null, maxAge: Int = -1): Int {
val urlString = url.toString()
val cookies = manager.getCookie(urlString) ?: return 0
val cookies = manager?.getCookie(urlString) ?: return 0
fun List<String>.filterNames(): List<String> {
return if (cookieNames != null) {
@ -49,6 +55,6 @@ class AndroidCookieJar : CookieJar {
}
fun removeAll() {
manager.removeAllCookies {}
manager?.removeAllCookies {}
}
}