pull/48/head
kunfei 5 years ago
parent 7f443158cf
commit f95f11857b
  1. 16
      app/src/main/java/io/legado/app/help/http/AjaxWebView.kt
  2. 8
      app/src/main/java/io/legado/app/help/http/HttpHelper.kt
  3. 18
      app/src/main/java/io/legado/app/model/WebBook.kt
  4. 5
      app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt

@ -38,7 +38,7 @@ class AjaxWebView {
mWebView = createAjaxWebView(params, this) mWebView = createAjaxWebView(params, this)
} }
MSG_SUCCESS -> { MSG_SUCCESS -> {
ajaxWebView.callback?.onResult(msg.obj as String) ajaxWebView.callback?.onResult(msg.obj as Response)
destroyWebView() destroyWebView()
} }
MSG_ERROR -> { MSG_ERROR -> {
@ -92,9 +92,8 @@ class AjaxWebView {
mHandler.obtainMessage(DESTROY_WEB_VIEW) mHandler.obtainMessage(DESTROY_WEB_VIEW)
} }
class AjaxParams(private val tag: String) { class AjaxParams(val url: String, private val tag: String) {
var requestMethod = RequestMethod.GET var requestMethod = RequestMethod.GET
var url: String? = null
var postData: ByteArray? = null var postData: ByteArray? = null
var headerMap: Map<String, String>? = null var headerMap: Map<String, String>? = null
var cookieStore: CookieStore? = null var cookieStore: CookieStore? = null
@ -133,7 +132,8 @@ class AjaxWebView {
params.setCookie(url) params.setCookie(url)
handler.postDelayed({ handler.postDelayed({
view.evaluateJavascript("document.documentElement.outerHTML") { view.evaluateJavascript("document.documentElement.outerHTML") {
handler.obtainMessage(MSG_SUCCESS, StringEscapeUtils.unescapeJson(it)) val content = StringEscapeUtils.unescapeJson(it)
handler.obtainMessage(MSG_SUCCESS, Response(url, content))
.sendToTarget() .sendToTarget()
} }
}, 1000) }, 1000)
@ -177,7 +177,7 @@ class AjaxWebView {
override fun onLoadResource(view: WebView, url: String) { override fun onLoadResource(view: WebView, url: String) {
params.sourceRegex?.let { params.sourceRegex?.let {
if (url.matches(it.toRegex())) { if (url.matches(it.toRegex())) {
handler.obtainMessage(MSG_SUCCESS, url) handler.obtainMessage(MSG_SUCCESS, Response(view.url ?: params.url, url))
.sendToTarget() .sendToTarget()
} }
} }
@ -230,14 +230,14 @@ class AjaxWebView {
webView: WebView, webView: WebView,
private val mJavaScript: String? private val mJavaScript: String?
) : Runnable { ) : Runnable {
private val mWebView: WeakReference<WebView> = WeakReference(webView) private val mWebView: WeakReference<WebView> = WeakReference(webView)
override fun run() { override fun run() {
mWebView.get()?.loadUrl("javascript:${mJavaScript ?: ""}") mWebView.get()?.loadUrl("javascript:${mJavaScript ?: ""}")
} }
} }
data class Response(val url: String, val content: String)
companion object { companion object {
const val MSG_AJAX_START = 0 const val MSG_AJAX_START = 0
const val MSG_SNIFF_START = 1 const val MSG_SNIFF_START = 1
@ -247,7 +247,7 @@ class AjaxWebView {
} }
abstract class Callback { abstract class Callback {
abstract fun onResult(result: String) abstract fun onResult(response: Response)
abstract fun onError(error: Throwable) abstract fun onError(error: Throwable)
} }
} }

@ -74,21 +74,21 @@ object HttpHelper {
} }
} }
suspend fun ajax(params: AjaxWebView.AjaxParams): String = suspend fun ajax(params: AjaxWebView.AjaxParams): AjaxWebView.Response =
suspendCancellableCoroutine { block -> suspendCancellableCoroutine { block ->
val webView = AjaxWebView() val webView = AjaxWebView()
block.invokeOnCancellation { block.invokeOnCancellation {
webView.destroyWebView() webView.destroyWebView()
} }
webView.callback = object : AjaxWebView.Callback() { webView.callback = object : AjaxWebView.Callback() {
override fun onResult(result: String) { override fun onResult(response: AjaxWebView.Response) {
if (!block.isCompleted) if (!block.isCompleted)
block.resume(result) block.resume(response)
} }
override fun onError(error: Throwable) { override fun onError(error: Throwable) {
if (!block.isCompleted) if (!block.isCompleted)
block.resume(error.localizedMessage) block.resume(AjaxWebView.Response(params.url, error.localizedMessage))
} }
} }
webView.load(params) webView.load(params)

@ -38,9 +38,11 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl, baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
var baseUrl = analyzeUrl.ruleUrl val baseUrl: String
val body = if (analyzeUrl.useWebView) { val body = if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl)
baseUrl = res.url
res.content
} else { } else {
val res = analyzeUrl.getResponseAwait() val res = analyzeUrl.getResponseAwait()
baseUrl = NetworkUtils.getUrl(res) baseUrl = NetworkUtils.getUrl(res)
@ -73,9 +75,11 @@ class WebBook(val bookSource: BookSource) {
baseUrl = sourceUrl, baseUrl = sourceUrl,
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
var baseUrl = analyzeUrl.ruleUrl val baseUrl: String
val body = if (analyzeUrl.useWebView) { val body = if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl)
baseUrl = res.url
res.content
} else { } else {
val res = analyzeUrl.getResponseAwait() val res = analyzeUrl.getResponseAwait()
baseUrl = NetworkUtils.getUrl(res) baseUrl = NetworkUtils.getUrl(res)
@ -112,7 +116,7 @@ class WebBook(val bookSource: BookSource) {
) )
if (analyzeUrl.useWebView) { if (analyzeUrl.useWebView) {
bookSource.getContentRule() bookSource.getContentRule()
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else { } else {
analyzeUrl.getResponseAwait().body() analyzeUrl.getResponseAwait().body()
} }
@ -142,7 +146,7 @@ class WebBook(val bookSource: BookSource) {
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
if (analyzeUrl.useWebView) { if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else { } else {
analyzeUrl.getResponseAwait().body() analyzeUrl.getResponseAwait().body()
} }
@ -176,7 +180,7 @@ class WebBook(val bookSource: BookSource) {
headerMapF = bookSource.getHeaderMap() headerMapF = bookSource.getHeaderMap()
) )
if (analyzeUrl.useWebView) { if (analyzeUrl.useWebView) {
analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content
} else { } else {
analyzeUrl.getResponseAwait().body() analyzeUrl.getResponseAwait().body()
} }

@ -295,9 +295,8 @@ class AnalyzeUrl(
tag: String, tag: String,
jsStr: String? = null, jsStr: String? = null,
sourceRegex: String? = null sourceRegex: String? = null
): String { ): AjaxWebView.Response {
val params = AjaxWebView.AjaxParams(tag) val params = AjaxWebView.AjaxParams(url, tag)
params.url = url
params.headerMap = headerMap params.headerMap = headerMap
params.requestMethod = method params.requestMethod = method
params.javaScript = jsStr params.javaScript = jsStr

Loading…
Cancel
Save