From a928d9fa0b1861e366a531209da4d4bb4d856325 Mon Sep 17 00:00:00 2001 From: arkon Date: Sun, 12 Jul 2020 22:40:51 -0400 Subject: [PATCH] Enable more WebView settings to better mimic regular browser --- .../tachiyomi/network/CloudflareInterceptor.kt | 3 ++- .../kanade/tachiyomi/ui/webview/WebViewActivity.kt | 6 +++--- .../eu/kanade/tachiyomi/util/system/WebViewUtil.kt | 13 +++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt index 57a86e4645..5d6bcb357c 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/network/CloudflareInterceptor.kt @@ -14,6 +14,7 @@ import androidx.webkit.WebViewFeature import eu.kanade.tachiyomi.R import eu.kanade.tachiyomi.source.online.HttpSource import eu.kanade.tachiyomi.util.system.isOutdated +import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.system.toast import java.io.IOException import java.util.concurrent.CountDownLatch @@ -85,7 +86,7 @@ class CloudflareInterceptor(private val context: Context) : Interceptor { handler.post { val webview = WebView(context) webView = webview - webview.settings.javaScriptEnabled = true + webview.setDefaultSettings() // Avoid sending empty User-Agent, Chromium WebView will reset to default if empty webview.settings.userAgentString = request.header("User-Agent") diff --git a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt index 2bcd8953a2..1328997be7 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/ui/webview/WebViewActivity.kt @@ -22,6 +22,7 @@ import eu.kanade.tachiyomi.ui.base.activity.BaseActivity import eu.kanade.tachiyomi.ui.main.ForceCloseActivity import eu.kanade.tachiyomi.util.system.getResourceColor import eu.kanade.tachiyomi.util.system.openInBrowser +import eu.kanade.tachiyomi.util.system.setDefaultSettings import eu.kanade.tachiyomi.util.system.toast import eu.kanade.tachiyomi.util.view.invisible import eu.kanade.tachiyomi.util.view.visible @@ -71,6 +72,8 @@ class WebViewActivity : BaseActivity() { binding.webview.settings.userAgentString = source.headers["User-Agent"] } + binding.webview.setDefaultSettings() + supportActionBar?.subtitle = url // Debug mode (chrome://inspect/#devices) @@ -78,9 +81,6 @@ class WebViewActivity : BaseActivity() { WebView.setWebContentsDebuggingEnabled(true) } - binding.webview.settings.javaScriptEnabled = true - binding.webview.settings.domStorageEnabled = true - binding.webview.webChromeClient = object : WebChromeClient() { override fun onProgressChanged(view: WebView?, newProgress: Int) { binding.progressBar.visible() diff --git a/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewUtil.kt b/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewUtil.kt index 6eaf6bc9f5..b92866e463 100644 --- a/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewUtil.kt +++ b/app/src/main/java/eu/kanade/tachiyomi/util/system/WebViewUtil.kt @@ -3,6 +3,7 @@ package eu.kanade.tachiyomi.util.system import android.content.Context import android.content.pm.PackageManager import android.webkit.CookieManager +import android.webkit.WebSettings import android.webkit.WebView object WebViewUtil { @@ -29,6 +30,18 @@ fun WebView.isOutdated(): Boolean { return getWebViewMajorVersion(this) < WebViewUtil.MINIMUM_WEBVIEW_VERSION } +fun WebView.setDefaultSettings() { + with(settings) { + javaScriptEnabled = true + domStorageEnabled = true + databaseEnabled = true + setAppCacheEnabled(true) + useWideViewPort = true + loadWithOverviewMode = true + cacheMode = WebSettings.LOAD_DEFAULT + } +} + // Based on https://stackoverflow.com/a/29218966 private fun getWebViewMajorVersion(webview: WebView): Int { val originalUA: String = webview.settings.userAgentString