diff --git a/app/src/main/java/io/legado/app/constant/AppConst.kt b/app/src/main/java/io/legado/app/constant/AppConst.kt index 0e1634219..69036d5d0 100644 --- a/app/src/main/java/io/legado/app/constant/AppConst.kt +++ b/app/src/main/java/io/legado/app/constant/AppConst.kt @@ -69,4 +69,11 @@ object AppConst { ) val sysElevation = appCtx.resources.getDimension(R.dimen.design_appbar_elevation).toInt() + + val darkWebViewJs by lazy { + """ + document.body.style.backgroundColor = "#222222"; + document.getElementsByTagName('body')[0].style.webkitTextFillColor = '#8a8a8a'; + """.trimIndent() + } } diff --git a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt index 5a4253b19..8bf30ba8d 100644 --- a/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt +++ b/app/src/main/java/io/legado/app/help/http/AjaxWebView.kt @@ -17,11 +17,7 @@ import java.lang.ref.WeakReference class AjaxWebView { var callback: Callback? = null - private var mHandler: AjaxHandler - - init { - mHandler = AjaxHandler(this) - } + private var mHandler: AjaxHandler = AjaxHandler(this) class AjaxHandler(private val ajaxWebView: AjaxWebView) : Handler(Looper.getMainLooper()) { 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 b72b3b1a8..eff2dda40 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 @@ -14,7 +14,9 @@ import androidx.activity.viewModels import androidx.core.view.size import io.legado.app.R import io.legado.app.base.VMBaseActivity +import io.legado.app.constant.AppConst import io.legado.app.databinding.ActivityRssReadBinding +import io.legado.app.help.AppConfig import io.legado.app.lib.theme.DrawableUtils import io.legado.app.lib.theme.primaryTextColor import io.legado.app.service.help.Download @@ -94,79 +96,17 @@ class ReadRssActivity : VMBaseActivity return super.onCompatOptionsItemSelected(item) } + @SuppressLint("SetJavaScriptEnabled") private fun initWebView() { - binding.webView.webChromeClient = object : WebChromeClient() { - override fun onShowCustomView(view: View?, callback: CustomViewCallback?) { - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR - binding.llView.invisible() - binding.customWebView.addView(view) - customWebViewCallback = callback - } - - override fun onHideCustomView() { - binding.customWebView.removeAllViews() - binding.llView.visible() - requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED - } - } - binding.webView.webViewClient = object : WebViewClient() { - override fun shouldOverrideUrlLoading( - view: WebView?, - request: WebResourceRequest? - ): Boolean { - request?.let { - if (it.url.scheme == "http" || it.url.scheme == "https") { - return false - } else if (it.url.scheme == "yuedu") { - when (it.url.host) { - "booksource" -> { - val intent = Intent( - this@ReadRssActivity, - ImportBookSourceActivity::class.java - ) - intent.data = it.url - startActivity(intent) - } - "rsssource" -> { - val intent = Intent( - this@ReadRssActivity, - ImportRssSourceActivity::class.java - ) - intent.data = it.url - startActivity(intent) - } - "replace" -> { - val intent = Intent( - this@ReadRssActivity, - ImportReplaceRuleActivity::class.java - ) - intent.data = it.url - startActivity(intent) - } - } - return true - } - openUrl(it.url) - } - return true - } - - @Suppress("DEPRECATION") - override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { - if (url?.startsWith("http", true) == true) { - return false - } - url?.let { - openUrl(it) - } - return true - } - } + binding.webView.webChromeClient = RssWebChromeClient() + binding.webView.webViewClient = RssWebViewClient() binding.webView.settings.apply { mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW domStorageEnabled = true allowContentAccess = true + //javaScriptEnabled = true } + upWebViewTheme() binding.webView.setOnLongClickListener { val hitTestResult = binding.webView.hitTestResult if (hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE || @@ -207,6 +147,7 @@ class ReadRssActivity : VMBaseActivity Download.start(this, downloadId, fileName) } } + } private fun saveImage() { @@ -254,6 +195,13 @@ class ReadRssActivity : VMBaseActivity } } + private fun upWebViewTheme() { + if (AppConfig.isNightTheme) { + binding.webView + .evaluateJavascript(AppConst.darkWebViewJs, null) + } + } + override fun upStarMenu() { if (viewModel.rssStar != null) { starMenuItem?.setIcon(R.drawable.ic_star) @@ -336,4 +284,78 @@ class ReadRssActivity : VMBaseActivity binding.webView.destroy() } + inner class RssWebChromeClient : WebChromeClient() { + override fun onShowCustomView(view: View?, callback: CustomViewCallback?) { + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR + binding.llView.invisible() + binding.customWebView.addView(view) + customWebViewCallback = callback + } + + override fun onHideCustomView() { + binding.customWebView.removeAllViews() + binding.llView.visible() + requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED + } + } + + inner class RssWebViewClient : WebViewClient() { + override fun shouldOverrideUrlLoading( + view: WebView?, + request: WebResourceRequest? + ): Boolean { + request?.let { + if (it.url.scheme == "http" || it.url.scheme == "https") { + return false + } else if (it.url.scheme == "yuedu") { + when (it.url.host) { + "booksource" -> { + val intent = Intent( + this@ReadRssActivity, + ImportBookSourceActivity::class.java + ) + intent.data = it.url + startActivity(intent) + } + "rsssource" -> { + val intent = Intent( + this@ReadRssActivity, + ImportRssSourceActivity::class.java + ) + intent.data = it.url + startActivity(intent) + } + "replace" -> { + val intent = Intent( + this@ReadRssActivity, + ImportReplaceRuleActivity::class.java + ) + intent.data = it.url + startActivity(intent) + } + } + return true + } + openUrl(it.url) + } + return true + } + + @Suppress("DEPRECATION") + override fun shouldOverrideUrlLoading(view: WebView?, url: String?): Boolean { + if (url?.startsWith("http", true) == true) { + return false + } + url?.let { + openUrl(it) + } + return true + } + + override fun onPageFinished(view: WebView?, url: String?) { + super.onPageFinished(view, url) + upWebViewTheme() + } + } + }