订阅添加夜间模式,需启用js,还不是很完善

pull/840/head
gedoor 4 years ago
parent 4fd945287f
commit b1698efc57
  1. 7
      app/src/main/java/io/legado/app/constant/AppConst.kt
  2. 6
      app/src/main/java/io/legado/app/help/http/AjaxWebView.kt
  3. 156
      app/src/main/java/io/legado/app/ui/rss/read/ReadRssActivity.kt

@ -69,4 +69,11 @@ object AppConst {
) )
val sysElevation = appCtx.resources.getDimension(R.dimen.design_appbar_elevation).toInt() 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()
}
} }

@ -17,11 +17,7 @@ import java.lang.ref.WeakReference
class AjaxWebView { class AjaxWebView {
var callback: Callback? = null var callback: Callback? = null
private var mHandler: AjaxHandler private var mHandler: AjaxHandler = AjaxHandler(this)
init {
mHandler = AjaxHandler(this)
}
class AjaxHandler(private val ajaxWebView: AjaxWebView) : Handler(Looper.getMainLooper()) { class AjaxHandler(private val ajaxWebView: AjaxWebView) : Handler(Looper.getMainLooper()) {

@ -14,7 +14,9 @@ import androidx.activity.viewModels
import androidx.core.view.size import androidx.core.view.size
import io.legado.app.R import io.legado.app.R
import io.legado.app.base.VMBaseActivity import io.legado.app.base.VMBaseActivity
import io.legado.app.constant.AppConst
import io.legado.app.databinding.ActivityRssReadBinding 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.DrawableUtils
import io.legado.app.lib.theme.primaryTextColor import io.legado.app.lib.theme.primaryTextColor
import io.legado.app.service.help.Download import io.legado.app.service.help.Download
@ -94,79 +96,17 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }
@SuppressLint("SetJavaScriptEnabled")
private fun initWebView() { private fun initWebView() {
binding.webView.webChromeClient = object : WebChromeClient() { binding.webView.webChromeClient = RssWebChromeClient()
override fun onShowCustomView(view: View?, callback: CustomViewCallback?) { binding.webView.webViewClient = RssWebViewClient()
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.settings.apply { binding.webView.settings.apply {
mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
domStorageEnabled = true domStorageEnabled = true
allowContentAccess = true allowContentAccess = true
//javaScriptEnabled = true
} }
upWebViewTheme()
binding.webView.setOnLongClickListener { binding.webView.setOnLongClickListener {
val hitTestResult = binding.webView.hitTestResult val hitTestResult = binding.webView.hitTestResult
if (hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE || if (hitTestResult.type == WebView.HitTestResult.IMAGE_TYPE ||
@ -207,6 +147,7 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
Download.start(this, downloadId, fileName) Download.start(this, downloadId, fileName)
} }
} }
} }
private fun saveImage() { private fun saveImage() {
@ -254,6 +195,13 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
} }
} }
private fun upWebViewTheme() {
if (AppConfig.isNightTheme) {
binding.webView
.evaluateJavascript(AppConst.darkWebViewJs, null)
}
}
override fun upStarMenu() { override fun upStarMenu() {
if (viewModel.rssStar != null) { if (viewModel.rssStar != null) {
starMenuItem?.setIcon(R.drawable.ic_star) starMenuItem?.setIcon(R.drawable.ic_star)
@ -336,4 +284,78 @@ class ReadRssActivity : VMBaseActivity<ActivityRssReadBinding, ReadRssViewModel>
binding.webView.destroy() 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()
}
}
} }

Loading…
Cancel
Save