From b1698efc57a9a143c3ae585180298dfc64be5358 Mon Sep 17 00:00:00 2001 From: gedoor Date: Fri, 5 Feb 2021 17:14:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A2=E9=98=85=E6=B7=BB=E5=8A=A0=E5=A4=9C?= =?UTF-8?q?=E9=97=B4=E6=A8=A1=E5=BC=8F,=E9=9C=80=E5=90=AF=E7=94=A8js,?= =?UTF-8?q?=E8=BF=98=E4=B8=8D=E6=98=AF=E5=BE=88=E5=AE=8C=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/legado/app/constant/AppConst.kt | 7 + .../io/legado/app/help/http/AjaxWebView.kt | 6 +- .../legado/app/ui/rss/read/ReadRssActivity.kt | 156 ++++++++++-------- 3 files changed, 97 insertions(+), 72 deletions(-) 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() + } + } + }