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 6d6c65428..662b71a6c 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 Response) + ajaxWebView.callback?.onResult(msg.obj as Res) destroyWebView() } MSG_ERROR -> { @@ -92,11 +92,10 @@ class AjaxWebView { mHandler.obtainMessage(DESTROY_WEB_VIEW) } - class AjaxParams(val url: String, private val tag: String) { + class AjaxParams(val url: String, private val tag: String?) { var requestMethod = RequestMethod.GET var postData: ByteArray? = null var headerMap: Map? = null - var cookieStore: CookieStore? = null var sourceRegex: String? = null var javaScript: String? = null @@ -116,9 +115,9 @@ class AjaxWebView { get() = !TextUtils.isEmpty(sourceRegex) fun setCookie(url: String) { - if (cookieStore != null) { + if (tag != null) { val cookie = CookieManager.getInstance().getCookie(url) - cookieStore?.setCookie(tag, cookie) + CookieStore.setCookie(tag, cookie) } } @@ -185,7 +184,7 @@ class AjaxWebView { mWebView.get()?.evaluateJavascript(mJavaScript) { if (it.isNotEmpty() && it != "null") { val content = StringEscapeUtils.unescapeJson(it) - handler.obtainMessage(MSG_SUCCESS, Response(url, content)) + handler.obtainMessage(MSG_SUCCESS, Res(url, content)) .sendToTarget() handler.removeCallbacks(this) return@evaluateJavascript @@ -211,7 +210,7 @@ class AjaxWebView { override fun onLoadResource(view: WebView, url: String) { params.sourceRegex?.let { if (url.matches(it.toRegex())) { - handler.obtainMessage(MSG_SUCCESS, Response(view.url ?: params.url, url)) + handler.obtainMessage(MSG_SUCCESS, Res(view.url ?: params.url, url)) .sendToTarget() } } @@ -270,8 +269,6 @@ class AjaxWebView { } } - data class Response(val url: String, val content: String) - companion object { const val MSG_AJAX_START = 0 const val MSG_SNIFF_START = 1 @@ -282,7 +279,7 @@ class AjaxWebView { } abstract class Callback { - abstract fun onResult(response: Response) + abstract fun onResult(response: Res) 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 a8a92a89b..da70b17a7 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): AjaxWebView.Response = + suspend fun ajax(params: AjaxWebView.AjaxParams): Res = suspendCancellableCoroutine { block -> val webView = AjaxWebView() block.invokeOnCancellation { webView.destroyWebView() } webView.callback = object : AjaxWebView.Callback() { - override fun onResult(response: AjaxWebView.Response) { + override fun onResult(response: Res) { if (!block.isCompleted) block.resume(response) } override fun onError(error: Throwable) { if (!block.isCompleted) - block.resume(AjaxWebView.Response(params.url, error.localizedMessage)) + block.resume(Res(params.url, error.localizedMessage)) } } webView.load(params) diff --git a/app/src/main/java/io/legado/app/help/http/Res.kt b/app/src/main/java/io/legado/app/help/http/Res.kt new file mode 100644 index 000000000..b0ba9ed24 --- /dev/null +++ b/app/src/main/java/io/legado/app/help/http/Res.kt @@ -0,0 +1,3 @@ +package io.legado.app.help.http + +data class Res(val url: String, val body: String?) \ No newline at end of file diff --git a/app/src/main/java/io/legado/app/model/Rss.kt b/app/src/main/java/io/legado/app/model/Rss.kt index 28c87c6e3..dcd49a3b3 100644 --- a/app/src/main/java/io/legado/app/model/Rss.kt +++ b/app/src/main/java/io/legado/app/model/Rss.kt @@ -18,11 +18,7 @@ object Rss { ): Coroutine> { return Coroutine.async(scope, context) { val analyzeUrl = AnalyzeUrl(rssSource.sourceUrl) - val body = if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(rssSource.sourceUrl).content - } else { - analyzeUrl.getResponseAwait().body() - } + val body = analyzeUrl.getResponseAwait(rssSource.sourceUrl).body RssParserByRule.parseXML(body, rssSource) } } 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 012e9ead7..d8bce710f 100644 --- a/app/src/main/java/io/legado/app/model/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/WebBook.kt @@ -10,7 +10,6 @@ import io.legado.app.model.webbook.BookChapterList import io.legado.app.model.webbook.BookContent import io.legado.app.model.webbook.BookInfo import io.legado.app.model.webbook.BookList -import io.legado.app.utils.NetworkUtils import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers import kotlin.coroutines.CoroutineContext @@ -38,21 +37,12 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap() ) - val baseUrl: String - val body = if (analyzeUrl.useWebView) { - val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) - baseUrl = res.url - res.content - } else { - val res = analyzeUrl.getResponseAwait() - baseUrl = NetworkUtils.getUrl(res) - res.body() - } + val res = analyzeUrl.getResponseAwait() BookList.analyzeBookList( - body, + res.body, bookSource, analyzeUrl, - baseUrl, + res.url, true ) } ?: arrayListOf() @@ -75,21 +65,12 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap() ) - val baseUrl: String - val body = if (analyzeUrl.useWebView) { - val res = analyzeUrl.getResultByWebView(bookSource.bookSourceUrl) - baseUrl = res.url - res.content - } else { - val res = analyzeUrl.getResponseAwait() - baseUrl = NetworkUtils.getUrl(res) - res.body() - } + val res = analyzeUrl.getResponseAwait() BookList.analyzeBookList( - body, + res.body, bookSource, analyzeUrl, - baseUrl, + res.url, false ) } @@ -114,12 +95,7 @@ class WebBook(val bookSource: BookSource) { baseUrl = sourceUrl, headerMapF = bookSource.getHeaderMap() ) - if (analyzeUrl.useWebView) { - bookSource.getContentRule() - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content - } else { - analyzeUrl.getResponseAwait().body() - } + analyzeUrl.getResponseAwait().body } BookInfo.analyzeBookInfo(book, body, bookSource, book.bookUrl) book @@ -139,17 +115,12 @@ class WebBook(val bookSource: BookSource) { val body = if (book.bookUrl == book.tocUrl && !book.tocHtml.isNullOrEmpty()) { book.tocHtml } else { - val analyzeUrl = AnalyzeUrl( + AnalyzeUrl( book = book, ruleUrl = book.tocUrl, baseUrl = book.bookUrl, headerMapF = bookSource.getHeaderMap() - ) - if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(bookSource.bookSourceUrl).content - } else { - analyzeUrl.getResponseAwait().body() - } + ).getResponseAwait().body } BookChapterList.analyzeChapterList(this, book, body, bookSource, book.tocUrl) } @@ -179,15 +150,11 @@ class WebBook(val bookSource: BookSource) { baseUrl = book.tocUrl, headerMapF = bookSource.getHeaderMap() ) - if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView( - bookSource.bookSourceUrl, - jsStr = bookSource.getContentRule().webJs, - sourceRegex = bookSource.getContentRule().sourceRegex - ).content - } else { - analyzeUrl.getResponseAwait().body() - } + analyzeUrl.getResponseAwait( + bookSource.bookSourceUrl, + jsStr = bookSource.getContentRule().webJs, + sourceRegex = bookSource.getContentRule().sourceRegex + ).body } BookContent.analyzeContent( this, 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 1705b1a50..95ead6ef4 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 @@ -13,13 +13,13 @@ import io.legado.app.help.JsExtensions import io.legado.app.help.http.AjaxWebView import io.legado.app.help.http.HttpHelper import io.legado.app.help.http.RequestMethod +import io.legado.app.help.http.Res import io.legado.app.utils.* import okhttp3.FormBody import okhttp3.MediaType.Companion.toMediaTypeOrNull import okhttp3.RequestBody import okhttp3.RequestBody.Companion.toRequestBody import retrofit2.Call -import retrofit2.Response import java.net.URLEncoder import java.util.* import java.util.regex.Pattern @@ -266,8 +266,21 @@ class AnalyzeUrl( } @Throws(Exception::class) - suspend fun getResponseAwait(): Response { - return when { + suspend fun getResponseAwait( + tag: String? = null, + jsStr: String? = null, + sourceRegex: String? = null + ): Res { + if (useWebView) { + val params = AjaxWebView.AjaxParams(url, tag) + params.headerMap = headerMap + params.requestMethod = method + params.javaScript = jsStr + params.sourceRegex = sourceRegex + params.postData = bodyTxt?.toByteArray() + return HttpHelper.ajax(params) + } + val res = when { method == RequestMethod.POST -> { if (fieldMap.isNotEmpty()) { HttpHelper @@ -290,20 +303,7 @@ class AnalyzeUrl( .getMapAsync(url, fieldMap, headerMap) .await() } - } - - suspend fun getResultByWebView( - tag: String, - jsStr: String? = null, - sourceRegex: String? = null - ): AjaxWebView.Response { - val params = AjaxWebView.AjaxParams(url, tag) - params.headerMap = headerMap - params.requestMethod = method - params.javaScript = jsStr - params.sourceRegex = sourceRegex - params.postData = bodyTxt?.toByteArray() - return HttpHelper.ajax(params) + return Res(NetworkUtils.getUrl(res), res.body()) } } diff --git a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt index 50419000b..91456e0c5 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookChapterList.kt @@ -50,7 +50,7 @@ object BookChapterList { AnalyzeUrl( ruleUrl = nextUrl, book = book, headerMapF = bookSource.getHeaderMap() ).getResponseAwait() - .body()?.let { nextBody -> + .body?.let { nextBody -> chapterData = analyzeChapterList( nextBody, nextUrl, tocRule, listRule, book, bookSource, log = false @@ -76,7 +76,7 @@ object BookChapterList { ruleUrl = item.nextUrl, book = book, headerMapF = bookSource.getHeaderMap() - ).getResponseAwait().body() ?: "" + ).getResponseAwait().body val nextChapterData = analyzeChapterList( nextBody, item.nextUrl, tocRule, listRule, book, bookSource ) @@ -109,7 +109,7 @@ object BookChapterList { private fun analyzeChapterList( - body: String, + body: String?, baseUrl: String, tocRule: TocRule, listRule: String, diff --git a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt index 6663d3b44..43e90c4cb 100644 --- a/app/src/main/java/io/legado/app/model/webbook/BookContent.kt +++ b/app/src/main/java/io/legado/app/model/webbook/BookContent.kt @@ -55,16 +55,11 @@ object BookContent { book = book, headerMapF = bookSource.getHeaderMap() ).getResponseAwait() - .body()?.let { nextBody -> + .body?.let { nextBody -> contentData = analyzeContent( - nextBody, - contentRule, - book, - bookChapter, - bookSource, - baseUrl, - false + nextBody, contentRule, book, + bookChapter, bookSource, baseUrl, false ) nextUrl = if (contentData.nextUrl.isNotEmpty()) contentData.nextUrl[0] else "" @@ -85,16 +80,11 @@ object BookContent { book = book, headerMapF = bookSource.getHeaderMap() ).getResponseAwait() - .body()?.let { + .body?.let { contentData = analyzeContent( - it, - contentRule, - book, - bookChapter, - bookSource, - item.nextUrl, - false + it, contentRule, book, bookChapter, + bookSource, item.nextUrl, false ) item.content = contentData.content } diff --git a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt index 5a24ef605..4bdcee197 100644 --- a/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt +++ b/app/src/main/java/io/legado/app/ui/rss/read/ReadRssViewModel.kt @@ -54,19 +54,16 @@ class ReadRssViewModel(application: Application) : BaseViewModel(application) { private fun loadContent(rssArticle: RssArticle, ruleContent: String) { execute { - val analyzeUrl = AnalyzeUrl(rssArticle.link, baseUrl = rssArticle.origin) - val content = if (analyzeUrl.useWebView) { - analyzeUrl.getResultByWebView(rssArticle.origin).content - } else analyzeUrl.getResponseAwait().body() - content?.let { body -> - AnalyzeRule().apply { - setContent( - body, - NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link) - ) - getString(ruleContent).let { content -> - contentLiveData.postValue(content) - } + val body = AnalyzeUrl(rssArticle.link, baseUrl = rssArticle.origin) + .getResponseAwait() + .body + AnalyzeRule().apply { + setContent( + body, + NetworkUtils.getAbsoluteURL(rssArticle.origin, rssArticle.link) + ) + getString(ruleContent).let { content -> + contentLiveData.postValue(content) } } }