pull/1352/head
gedoor 4 years ago
parent 8884ba76f8
commit fafa8e08fc
  1. 15
      app/src/main/java/io/legado/app/help/IntentData.kt
  2. 9
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt
  3. 13
      app/src/main/java/io/legado/app/ui/book/read/ReadMenu.kt
  4. 15
      app/src/main/java/io/legado/app/ui/web/WebViewActivity.kt
  5. 29
      app/src/main/java/io/legado/app/ui/web/WebViewModel.kt

@ -4,13 +4,24 @@ object IntentData {
private val bigData: MutableMap<String, Any> = mutableMapOf() private val bigData: MutableMap<String, Any> = mutableMapOf()
fun put(data: Any, tag: String = ""): String { @Synchronized
val key = tag + System.currentTimeMillis() fun put(key: String, data: Any?) {
data?.let {
bigData[key] = data bigData[key] = data
}
}
@Synchronized
fun put(data: Any?): String {
val key = System.currentTimeMillis().toString()
data?.let {
bigData[key] = data
}
return key return key
} }
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
@Synchronized
fun <T> get(key: String?): T? { fun <T> get(key: String?): T? {
if (key == null) return null if (key == null) return null
val data = bigData[key] val data = bigData[key]

@ -322,14 +322,15 @@ class AnalyzeUrl(
*/ */
suspend fun getStrResponse( suspend fun getStrResponse(
jsStr: String? = null, jsStr: String? = null,
sourceRegex: String? = null sourceRegex: String? = null,
useWebView: Boolean = true,
): StrResponse { ): StrResponse {
if (type != null) { if (type != null) {
return StrResponse(url, StringUtils.byteToHexString(getByteArray())) return StrResponse(url, StringUtils.byteToHexString(getByteArray()))
} }
judgmentConcurrent() judgmentConcurrent()
setCookie(source?.getKey()) setCookie(source?.getKey())
if (useWebView) { if (this.useWebView && useWebView) {
return when (method) { return when (method) {
RequestMethod.POST -> { RequestMethod.POST -> {
val body = getProxyClient(proxy).newCallStrResponse(retry) { val body = getProxyClient(proxy).newCallStrResponse(retry) {
@ -445,6 +446,10 @@ class AnalyzeUrl(
return headerMap[UA_NAME] ?: AppConfig.userAgent return headerMap[UA_NAME] ?: AppConfig.userAgent
} }
fun isPost(): Boolean {
return method == RequestMethod.POST
}
override fun getSource(): BaseSource? { override fun getSource(): BaseSource? {
return source return source
} }

@ -14,12 +14,10 @@ import androidx.core.view.isVisible
import io.legado.app.R import io.legado.app.R
import io.legado.app.constant.PreferKey import io.legado.app.constant.PreferKey
import io.legado.app.databinding.ViewReadMenuBinding import io.legado.app.databinding.ViewReadMenuBinding
import io.legado.app.help.AppConfig import io.legado.app.help.*
import io.legado.app.help.LocalConfig
import io.legado.app.help.ReadBookConfig
import io.legado.app.help.ThemeConfig
import io.legado.app.lib.theme.* import io.legado.app.lib.theme.*
import io.legado.app.model.ReadBook import io.legado.app.model.ReadBook
import io.legado.app.ui.web.WebViewActivity
import io.legado.app.ui.widget.seekbar.SeekBarChangeListener import io.legado.app.ui.widget.seekbar.SeekBarChangeListener
import io.legado.app.utils.* import io.legado.app.utils.*
import splitties.views.onLongClick import splitties.views.onLongClick
@ -142,7 +140,12 @@ class ReadMenu @JvmOverloads constructor(
callBack.openSourceEditActivity() callBack.openSourceEditActivity()
} }
tvChapterUrl.setOnClickListener { tvChapterUrl.setOnClickListener {
context.sendToClip(binding.tvChapterUrl.text.toString()) context.startActivity<WebViewActivity> {
val url = tvChapterUrl.text.toString()
putExtra("title", tvChapterName.text)
putExtra("url", url)
IntentData.put(url, ReadBook.bookSource?.getHeaderMap(true))
}
} }
//登录 //登录
tvLogin.setOnClickListener { tvLogin.setOnClickListener {

@ -36,8 +36,17 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
} }
override fun onActivityCreated(savedInstanceState: Bundle?) { override fun onActivityCreated(savedInstanceState: Bundle?) {
binding.titleBar.title = intent.getStringExtra("title") ?: getString(R.string.loading)
initWebView() initWebView()
viewModel.initData(intent) {
val url = viewModel.baseUrl
val html = viewModel.html
if (html.isNullOrEmpty()) {
binding.webView.loadUrl(url, viewModel.headerMap)
} else {
binding.webView.loadDataWithBaseURL(url, html, "text/html", "utf-8", url)
}
}
} }
override fun onCompatCreateOptionsMenu(menu: Menu): Boolean { override fun onCompatCreateOptionsMenu(menu: Menu): Boolean {
@ -47,8 +56,8 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
override fun onCompatOptionsItemSelected(item: MenuItem): Boolean { override fun onCompatOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) { when (item.itemId) {
R.id.menu_open_in_browser -> openUrl("") R.id.menu_open_in_browser -> openUrl(viewModel.baseUrl)
R.id.menu_copy_url -> sendToClip("") R.id.menu_copy_url -> sendToClip(viewModel.baseUrl)
} }
return super.onCompatOptionsItemSelected(item) return super.onCompatOptionsItemSelected(item)
} }

@ -1,22 +1,47 @@
package io.legado.app.ui.web package io.legado.app.ui.web
import android.app.Application import android.app.Application
import android.content.Intent
import android.net.Uri import android.net.Uri
import android.util.Base64 import android.util.Base64
import android.webkit.URLUtil import android.webkit.URLUtil
import androidx.documentfile.provider.DocumentFile import androidx.documentfile.provider.DocumentFile
import io.legado.app.base.BaseViewModel import io.legado.app.base.BaseViewModel
import io.legado.app.constant.AppConst import io.legado.app.constant.AppConst
import io.legado.app.help.IntentData
import io.legado.app.help.http.newCall import io.legado.app.help.http.newCall
import io.legado.app.help.http.okHttpClient import io.legado.app.help.http.okHttpClient
import io.legado.app.model.NoStackTraceException
import io.legado.app.model.analyzeRule.AnalyzeUrl
import io.legado.app.utils.* import io.legado.app.utils.*
import java.io.File import java.io.File
import java.util.* import java.util.*
class WebViewModel(application: Application) : BaseViewModel(application) { class WebViewModel(application: Application) : BaseViewModel(application) {
var baseUrl: String = ""
var html: String? = null
val headerMap: HashMap<String, String> = hashMapOf()
fun initData(url: String, header: Map<String, String>?) { fun initData(
intent: Intent,
success: () -> Unit
) {
execute {
val url = intent.getStringExtra("url")
?: throw NoStackTraceException("url不能为空")
val headerMapF = IntentData.get<Map<String, String>>(url)
val analyzeUrl = AnalyzeUrl(url, headerMapF = headerMapF)
baseUrl = analyzeUrl.url
headerMap.putAll(analyzeUrl.headerMap)
if (analyzeUrl.isPost()) {
html = analyzeUrl.getStrResponse(useWebView = false).body
}
}.onSuccess {
success.invoke()
}.onError {
context.toastOnUi("error\n${it.localizedMessage}")
it.printOnDebug()
}
} }
fun saveImage(webPic: String?, path: String) { fun saveImage(webPic: String?, path: String) {

Loading…
Cancel
Save