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 4cd8a3dc5..d383c4d62 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 @@ -38,7 +38,7 @@ class AjaxWebView { mWebView = createAjaxWebView(params, this) } MSG_SUCCESS -> { - ajaxWebView.callback?.onResult(msg.obj as String) + ajaxWebView.callback?.onResult(msg.obj as Response) destroyWebView() } MSG_ERROR -> { @@ -92,9 +92,8 @@ class AjaxWebView { 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 url: String? = null var postData: ByteArray? = null var headerMap: Map? = null var cookieStore: CookieStore? = null @@ -133,7 +132,8 @@ class AjaxWebView { params.setCookie(url) handler.postDelayed({ 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() } }, 1000) @@ -177,7 +177,7 @@ class AjaxWebView { override fun onLoadResource(view: WebView, url: String) { params.sourceRegex?.let { if (url.matches(it.toRegex())) { - handler.obtainMessage(MSG_SUCCESS, url) + handler.obtainMessage(MSG_SUCCESS, Response(view.url ?: params.url, url)) .sendToTarget() } } @@ -230,14 +230,14 @@ class AjaxWebView { webView: WebView, private val mJavaScript: String? ) : Runnable { - private val mWebView: WeakReference = WeakReference(webView) - override fun run() { mWebView.get()?.loadUrl("javascript:${mJavaScript ?: ""}") } } + data class Response(val url: String, val content: String) + companion object { const val MSG_AJAX_START = 0 const val MSG_SNIFF_START = 1 @@ -247,7 +247,7 @@ class AjaxWebView { } abstract class Callback { - abstract fun onResult(result: String) + abstract fun onResult(response: Response) abstract fun onError(error: Throwable) } } \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt index 1053bc3b8..a8a92a89b 100644 --- a/app/src/main/java/io/legado/app/help/http/HttpHelper.kt +++ b/app/src/main/java/io/legado/app/help/http/HttpHelper.kt @@ -74,21 +74,21 @@ object HttpHelper { } } - suspend fun ajax(params: AjaxWebView.AjaxParams): String = + suspend fun ajax(params: AjaxWebView.AjaxParams): AjaxWebView.Response = suspendCancellableCoroutine { block -> val webView = AjaxWebView() block.invokeOnCancellation { webView.destroyWebView() } webView.callback = object : AjaxWebView.Callback() { - override fun onResult(result: String) { + override fun onResult(response: AjaxWebView.Response) { if (!block.isCompleted) - block.resume(result) + block.resume(response) } override fun onError(error: Throwable) { if (!block.isCompleted) - block.resume(error.localizedMessage) + block.resume(AjaxWebView.Response(params.url, error.localizedMessage)) } } webView.load(params) diff --git a/app/src/main/java/io/legado/app/model/WebBook.kt b/app/src/main/java/io/legado/app/model/WebBook.kt index 06d3af1f7..9eff0082e 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -38,9 +38,11 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap() ) - var baseUrl = analyzeUrl.ruleUrl + val baseUrl: String val body = if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + baseUrl = res.url + res.content } else { val res = analyzeUrl.getResponseAwait() baseUrl = NetworkUtils.getUrl(res) @@ -73,9 +75,11 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap() ) - var baseUrl = analyzeUrl.ruleUrl + val baseUrl: String val body = if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + baseUrl = res.url + res.content } else { val res = analyzeUrl.getResponseAwait() baseUrl = NetworkUtils.getUrl(res) @@ -112,7 +116,7 @@ class WebBook(val bookSource: BookSource) { ) if (analyzeUrl.useWebView) { bookSource.getContentRule() - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content } else { analyzeUrl.getResponseAwait().body() } @@ -142,7 +146,7 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap() ) if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content } else { analyzeUrl.getResponseAwait().body() } @@ -176,7 +180,7 @@ class WebBook(val bookSource: BookSource) { headerMapF = bookSource.getHeaderMap() ) if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) + analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content } else { analyzeUrl.getResponseAwait().body() } diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt index c52014ca9..f32e41549 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeUrl.kt @@ -295,9 +295,8 @@ class AnalyzeUrl( tag: String, jsStr: String? = null, sourceRegex: String? = null - ): String { - val params = AjaxWebView.AjaxParams(tag) - params.url = url + ): AjaxWebView.Response { + val params = AjaxWebView.AjaxParams(url, tag) params.headerMap = headerMap params.requestMethod = method params.javaScript = jsStr