Merge pull request #1778 from Xwite/master

内置浏览器cookie header js
pull/1781/head
kunfei 3 years ago committed by GitHub
commit c7858c0177
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 5
      app/src/main/assets/help/jsHelp.md
  2. 7
      app/src/main/assets/updateLog.md
  3. 18
      app/src/main/java/io/legado/app/api/controller/BookController.kt
  4. 15
      app/src/main/java/io/legado/app/help/JsExtensions.kt
  5. 4
      app/src/main/java/io/legado/app/ui/book/read/page/provider/ImageProvider.kt
  6. 19
      app/src/main/java/io/legado/app/ui/browser/WebViewActivity.kt
  7. 8
      app/src/main/java/io/legado/app/ui/login/WebViewLoginFragment.kt

@ -68,6 +68,11 @@ java.webView(html: String?, url: String?, js: String?): String
java.log(msg) java.log(msg)
java.logType(var) java.logType(var)
``` ```
* 弹窗提示
```
java.longToast(msg: Any?)
java.toast(msg: Any?)
```
* 从网络(由java.cacheFile实现)、本地导入JavaScript脚本 * 从网络(由java.cacheFile实现)、本地导入JavaScript脚本
``` ```
{{java.importScript(url)}} {{java.importScript(url)}}

@ -11,7 +11,12 @@
* 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。 * 正文出现缺字漏字、内容缺失、排版错乱等情况,有可能是净化规则或简繁转换出现问题。
* 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源! * 漫画源看书显示乱码,**阅读与其他软件的源并不通用**,请导入阅读的支持的漫画源!
**2022/04/012** **2022/04/13**
* 修复解码正文图片报错,添加解码日志
* js文档:java.toast java.longToast
**2022/04/12**
* 更新cronet: 100.0.4896.88 * 更新cronet: 100.0.4896.88
* 恢复epub加载方式 * 恢复epub加载方式

@ -22,6 +22,7 @@ import io.legado.app.model.localBook.LocalBook
import io.legado.app.model.localBook.UmdFile import io.legado.app.model.localBook.UmdFile
import io.legado.app.model.webBook.WebBook import io.legado.app.model.webBook.WebBook
import io.legado.app.utils.* import io.legado.app.utils.*
import io.legado.app.ui.book.read.page.provider.ImageProvider
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import splitties.init.appCtx import splitties.init.appCtx
@ -79,23 +80,16 @@ object BookController {
?: return returnData.setErrorMsg("bookUrl为空") ?: return returnData.setErrorMsg("bookUrl为空")
val src = parameters["path"]?.firstOrNull() val src = parameters["path"]?.firstOrNull()
?: return returnData.setErrorMsg("图片链接为空") ?: return returnData.setErrorMsg("图片链接为空")
val width = parameters["width"]?.firstOrNull()?.toInt() ?: 640
if (this.bookUrl != bookUrl) { if (this.bookUrl != bookUrl) {
this.book = appDb.bookDao.getBook(bookUrl) this.book = appDb.bookDao.getBook(bookUrl)
?: return returnData.setErrorMsg("bookUrl不对") ?: return returnData.setErrorMsg("bookUrl不对")
} this.bookSource = appDb.bookSourceDao.getBookSource(book.origin)
val vFile = BookHelp.getImage(book, src)
if (!vFile.exists()) {
if (this.bookUrl != bookUrl) {
this.bookSource = appDb.bookSourceDao.getBookSource(book.origin)
}
runBlocking {
BookHelp.saveImage(bookSource, book, src)
}
} }
this.bookUrl = bookUrl this.bookUrl = bookUrl
return returnData.setData( return ImageProvider.getImage(book, src, bookSource, width, width)?.let {
ImageLoader.loadBitmap(appCtx, vFile.absolutePath).submit().get() returnData.setData(it)
) } ?: returnData.setErrorMsg("图片加载失败或不存在")
} }
/** /**

@ -34,6 +34,7 @@ import java.util.zip.ZipInputStream
/** /**
* js扩展类, 在js中通过java变量调用 * js扩展类, 在js中通过java变量调用
* 添加方法请更新文档/legado/app/src/main/assets/help/JsHelp.md
* 所有对于文件的读写删操作都是相对路径,只能操作阅读缓存内的文件 * 所有对于文件的读写删操作都是相对路径,只能操作阅读缓存内的文件
* /android/data/{package}/cache/... * /android/data/{package}/cache/...
*/ */
@ -526,6 +527,20 @@ interface JsExtensions {
return contentArray.joinToString("") return contentArray.joinToString("")
} }
/**
* 弹窗提示
*/
fun toast(msg: Any?) {
appCtx.toastOnUi(msg.toString())
}
/**
* 弹窗提示 停留时间较长
*/
fun longToast(msg: Any?) {
appCtx.longToastOnUi(msg.toString())
}
/** /**
* 输出调试日志 * 输出调试日志
*/ */

@ -82,7 +82,7 @@ object ImageProvider {
.get() .get()
} catch (e: Exception) { } catch (e: Exception) {
Coroutine.async { Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败", e) putDebug("${vFile.absolutePath} 解码失败\n${e.toString()}", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) { if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除") putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete() vFile.delete()
@ -106,7 +106,7 @@ object ImageProvider {
.get() .get()
} catch (e: Exception) { } catch (e: Exception) {
Coroutine.async { Coroutine.async {
putDebug("${vFile.absolutePath} 解码失败", e) putDebug("${vFile.absolutePath} 解码失败\n${e.toString()}", e)
if (FileUtils.readText(vFile.absolutePath).isXml()) { if (FileUtils.readText(vFile.absolutePath).isXml()) {
putDebug("${vFile.absolutePath}为xml,自动删除") putDebug("${vFile.absolutePath}为xml,自动删除")
vFile.delete() vFile.delete()

@ -15,8 +15,10 @@ import androidx.webkit.WebSettingsCompat
import androidx.webkit.WebViewFeature import androidx.webkit.WebViewFeature
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.ActivityWebViewBinding import io.legado.app.databinding.ActivityWebViewBinding
import io.legado.app.help.config.AppConfig import io.legado.app.help.config.AppConfig
import io.legado.app.help.http.CookieStore
import io.legado.app.lib.dialogs.SelectItem import io.legado.app.lib.dialogs.SelectItem
import io.legado.app.model.Download import io.legado.app.model.Download
import io.legado.app.ui.association.OnLineImportActivity import io.legado.app.ui.association.OnLineImportActivity
@ -41,12 +43,13 @@ 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) binding.titleBar.title = intent.getStringExtra("title") ?: getString(R.string.loading)
initWebView()
viewModel.initData(intent) { viewModel.initData(intent) {
val url = viewModel.baseUrl val url = viewModel.baseUrl
val headerMap = viewModel.headerMap
initWebView(url, headerMap)
val html = viewModel.html val html = viewModel.html
if (html.isNullOrEmpty()) { if (html.isNullOrEmpty()) {
binding.webView.loadUrl(url, viewModel.headerMap) binding.webView.loadUrl(url, headerMap)
} else { } else {
binding.webView.loadDataWithBaseURL(url, html, "text/html", "utf-8", url) binding.webView.loadDataWithBaseURL(url, html, "text/html", "utf-8", url)
} }
@ -67,7 +70,7 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
} }
@SuppressLint("JavascriptInterface") @SuppressLint("JavascriptInterface")
private fun initWebView() { private fun initWebView(url: String, headerMap: HashMap<String, String>) {
binding.webView.webChromeClient = CustomWebChromeClient() binding.webView.webChromeClient = CustomWebChromeClient()
binding.webView.webViewClient = CustomWebViewClient() binding.webView.webViewClient = CustomWebViewClient()
binding.webView.settings.apply { binding.webView.settings.apply {
@ -76,7 +79,13 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
allowContentAccess = true allowContentAccess = true
useWideViewPort = true useWideViewPort = true
loadWithOverviewMode = true loadWithOverviewMode = true
javaScriptEnabled = true
headerMap[AppConst.UA_NAME]?.let {
userAgentString = it
}
} }
val cookieManager = CookieManager.getInstance()
cookieManager.setCookie(url, CookieStore.getCookie(url))
binding.webView.addJavascriptInterface(this, "app") binding.webView.addJavascriptInterface(this, "app")
upWebViewTheme() upWebViewTheme()
binding.webView.setOnLongClickListener { binding.webView.setOnLongClickListener {
@ -207,6 +216,10 @@ class WebViewActivity : VMBaseActivity<ActivityWebViewBinding, WebViewModel>() {
override fun onPageFinished(view: WebView?, url: String?) { override fun onPageFinished(view: WebView?, url: String?) {
super.onPageFinished(view, url) super.onPageFinished(view, url)
val cookieManager = CookieManager.getInstance()
url?.let {
CookieStore.setCookie(it, cookieManager.getCookie(it))
}
view?.title?.let { title -> view?.title?.let { title ->
if (title != url && title != view.url && title.isNotBlank()) { if (title != url && title != view.url && title.isNotBlank()) {
binding.titleBar.title = title binding.titleBar.title = title

@ -44,8 +44,10 @@ class WebViewLoginFragment : BaseFragment(R.layout.fragment_web_view_login) {
if (!checking) { if (!checking) {
checking = true checking = true
binding.titleBar.snackbar(R.string.check_host_cookie) binding.titleBar.snackbar(R.string.check_host_cookie)
viewModel.source?.loginUrl?.let { viewModel.source?.let { source ->
binding.webView.loadUrl(it) source.loginUrl?.let {
binding.webView.loadUrl(it, source.getHeaderMap(true))
}
} }
} }
} }
@ -82,7 +84,7 @@ class WebViewLoginFragment : BaseFragment(R.layout.fragment_web_view_login) {
} }
} }
source.loginUrl?.let { source.loginUrl?.let {
binding.webView.loadUrl(it) binding.webView.loadUrl(it, source.getHeaderMap(true))
} }
} }

Loading…
Cancel
Save