diff --git a/app/src/main/java/io/legado/app/help/JsExtensions.kt b/app/src/main/java/io/legado/app/help/JsExtensions.kt index bf4504533..b7030d760 100644 --- a/app/src/main/java/io/legado/app/help/JsExtensions.kt +++ b/app/src/main/java/io/legado/app/help/JsExtensions.kt @@ -40,7 +40,7 @@ interface JsExtensions { return runBlocking { kotlin.runCatching { val analyzeUrl = AnalyzeUrl(urlStr) - analyzeUrl.getStrResponse(urlStr).body + analyzeUrl.getStrResponse().body }.onFailure { it.printStackTrace() }.getOrElse { @@ -58,7 +58,7 @@ interface JsExtensions { async(IO) { val url = urlList[it] val analyzeUrl = AnalyzeUrl(url) - analyzeUrl.getStrResponse(url) + analyzeUrl.getStrResponse() } } val resArray = Array(urlList.size) { @@ -75,7 +75,7 @@ interface JsExtensions { return runBlocking { val analyzeUrl = AnalyzeUrl(urlStr) kotlin.runCatching { - analyzeUrl.getStrResponse(urlStr) + analyzeUrl.getStrResponse() }.onFailure { it.printStackTrace() }.getOrElse { @@ -89,7 +89,7 @@ interface JsExtensions { val headerMap = GSON.fromJsonObject>(header) val analyzeUrl = AnalyzeUrl(urlStr, headerMapF = headerMap) kotlin.runCatching { - analyzeUrl.getStrResponse(urlStr) + analyzeUrl.getStrResponse() }.onFailure { it.printStackTrace() }.getOrElse { diff --git a/app/src/main/java/io/legado/app/model/ReadBook.kt b/app/src/main/java/io/legado/app/model/ReadBook.kt index b0381fee2..2c75ed3dd 100644 --- a/app/src/main/java/io/legado/app/model/ReadBook.kt +++ b/app/src/main/java/io/legado/app/model/ReadBook.kt @@ -136,14 +136,6 @@ object ReadBook : CoroutineScope by MainScope() { saveRead() callBack?.upView() curPageChanged() - Coroutine.async { - //预下载 - val maxChapterIndex = durChapterIndex + AppConfig.preDownloadNum - for (i in durChapterIndex.plus(2)..maxChapterIndex) { - delay(1000) - download(i) - } - } return true } else { return false @@ -169,14 +161,6 @@ object ReadBook : CoroutineScope by MainScope() { saveRead() callBack?.upView() curPageChanged() - Coroutine.async { - //预下载 - val minChapterIndex = durChapterIndex - 5 - for (i in durChapterIndex.minus(2) downTo minChapterIndex) { - delay(1000) - download(i) - } - } return true } else { return false @@ -207,6 +191,7 @@ object ReadBook : CoroutineScope by MainScope() { readAloud(!BaseReadAloudService.pause) } upReadStartTime() + preDownload() } /** @@ -436,6 +421,25 @@ object ReadBook : CoroutineScope by MainScope() { } } + /** + * 预下载 + */ + private fun preDownload() { + Coroutine.async { + //预下载 + val maxChapterIndex = durChapterIndex + AppConfig.preDownloadNum + for (i in durChapterIndex.plus(2)..maxChapterIndex) { + delay(1000) + download(i) + } + val minChapterIndex = durChapterIndex - 5 + for (i in durChapterIndex.minus(2) downTo minChapterIndex) { + delay(1000) + download(i) + } + } + } + interface CallBack { fun loadChapterList(book: Book) diff --git a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt index 47f0787f2..2d9064f8c 100644 --- a/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt +++ b/app/src/main/java/io/legado/app/model/analyzeRule/AnalyzeRule.kt @@ -659,7 +659,7 @@ class AnalyzeRule( return runBlocking { kotlin.runCatching { val analyzeUrl = AnalyzeUrl(urlStr, book = book, source = source) - analyzeUrl.getStrResponse(urlStr).body + analyzeUrl.getStrResponse().body }.onFailure { it.printStackTrace() }.getOrElse { 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 d8a6caff0..967cf6c52 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 @@ -68,13 +68,17 @@ class AnalyzeUrl( headerMap.remove("proxy") } } - //替换参数 + //执行@js, analyzeJs() + //替换参数 replaceKeyPageJs() //处理URL initUrl() } + /** + * 执行@js, + */ private fun analyzeJs() { var start = 0 var tmp: String @@ -104,9 +108,7 @@ class AnalyzeUrl( private fun replaceKeyPageJs() { //先替换内嵌规则再替换页数规则,避免内嵌规则中存在大于小于号时,规则被切错 //js if (ruleUrl.contains("{{") && ruleUrl.contains("}}")) { - val analyze = RuleAnalyzer(ruleUrl) //创建解析 - //替换所有内嵌{{js}} val url = analyze.innerRule("{{", "}}") { val jsEval = evalJS(it) ?: "" @@ -262,14 +264,13 @@ class AnalyzeUrl( } suspend fun getStrResponse( - tag: String, jsStr: String? = null, sourceRegex: String? = null, ): StrResponse { if (type != null) { - return StrResponse(url, StringUtils.byteToHexString(getByteArray(tag))) + return StrResponse(url, StringUtils.byteToHexString(getByteArray())) } - setCookie(tag) + setCookie(source?.getStoreUrl()) if (useWebView) { val params = AjaxWebView.AjaxParams(url) params.headerMap = headerMap @@ -277,7 +278,7 @@ class AnalyzeUrl( params.javaScript = jsStr params.sourceRegex = sourceRegex params.postData = body?.toByteArray() - params.tag = tag + params.tag = source?.getStoreUrl() return getWebViewSrc(params) } return getProxyClient(proxy).newCallStrResponse(retry) { @@ -296,8 +297,8 @@ class AnalyzeUrl( } } - suspend fun getByteArray(tag: String? = null): ByteArray { - setCookie(tag) + suspend fun getByteArray(): ByteArray { + setCookie(source?.getStoreUrl()) @Suppress("BlockingMethodInNonBlockingContext") return getProxyClient(proxy).newCall(retry) { addHeaders(headerMap) diff --git a/app/src/main/java/io/legado/app/model/rss/Rss.kt b/app/src/main/java/io/legado/app/model/rss/Rss.kt index 3207fcc3c..e36eb9fcc 100644 --- a/app/src/main/java/io/legado/app/model/rss/Rss.kt +++ b/app/src/main/java/io/legado/app/model/rss/Rss.kt @@ -39,9 +39,10 @@ object Rss { sortUrl, page = page, ruleData = ruleData, + source = rssSource, headerMapF = rssSource.getHeaderMap() ) - val body = analyzeUrl.getStrResponse(rssSource.sourceUrl).body + val body = analyzeUrl.getStrResponse().body return RssParserByRule.parseXML(sortName, sortUrl, body, rssSource, ruleData) } @@ -66,9 +67,10 @@ object Rss { rssArticle.link, baseUrl = rssArticle.origin, ruleData = rssArticle, + source = rssSource, headerMapF = rssSource.getHeaderMap() ) - val body = analyzeUrl.getStrResponse(rssArticle.origin).body + val body = analyzeUrl.getStrResponse().body Debug.log(rssSource.sourceUrl, "≡获取成功:${rssSource.sourceUrl}") Debug.log(rssSource.sourceUrl, body, state = 20) val analyzeRule = AnalyzeRule(rssArticle, rssSource) 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 5e8022574..518e7ffab 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 @@ -61,7 +61,7 @@ object BookChapterList { book = book, source = bookSource, headerMapF = bookSource.getHeaderMap() - ).getStrResponse(bookSource.bookSourceUrl).body?.let { nextBody -> + ).getStrResponse().body?.let { nextBody -> chapterData = analyzeChapterList( scope, book, nextUrl, nextUrl, nextBody, tocRule, listRule, bookSource @@ -84,7 +84,7 @@ object BookChapterList { source = bookSource, headerMapF = bookSource.getHeaderMap() ) - val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + val res = analyzeUrl.getStrResponse() analyzeChapterList( this, book, urlStr, res.url, res.body!!, tocRule, listRule, bookSource, false 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 3051d11be..38ba8c698 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 @@ -68,7 +68,7 @@ object BookContent { book = book, source = bookSource, headerMapF = bookSource.getHeaderMap() - ).getStrResponse(bookSource.bookSourceUrl) + ).getStrResponse() res.body?.let { nextBody -> contentData = analyzeContent( book, nextUrl, res.url, nextBody, contentRule, @@ -92,7 +92,7 @@ object BookContent { source = bookSource, headerMapF = bookSource.getHeaderMap() ) - val res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + val res = analyzeUrl.getStrResponse() analyzeContent( book, urlStr, res.url, res.body!!, contentRule, bookChapter, bookSource, mNextChapterUrl, false diff --git a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt index efa1b5632..4e811fc65 100644 --- a/app/src/main/java/io/legado/app/model/webBook/WebBook.kt +++ b/app/src/main/java/io/legado/app/model/webBook/WebBook.kt @@ -74,7 +74,7 @@ object WebBook { book = variableBook, source = bookSource ) - var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + var res = analyzeUrl.getStrResponse() //检测书源是否已登录 bookSource.loginCheckJs?.let { checkJs -> if (checkJs.isNotBlank()) { @@ -124,7 +124,7 @@ object WebBook { source = bookSource, headerMapF = bookSource.getHeaderMap(true) ) - var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + var res = analyzeUrl.getStrResponse() //检测书源是否已登录 bookSource.loginCheckJs?.let { checkJs -> if (checkJs.isNotBlank()) { @@ -182,7 +182,7 @@ object WebBook { source = bookSource, headerMapF = bookSource.getHeaderMap(true) ) - var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + var res = analyzeUrl.getStrResponse() //检测书源是否已登录 bookSource.loginCheckJs?.let { checkJs -> if (checkJs.isNotBlank()) { @@ -239,7 +239,7 @@ object WebBook { source = bookSource, headerMapF = bookSource.getHeaderMap(true) ) - var res = analyzeUrl.getStrResponse(bookSource.bookSourceUrl) + var res = analyzeUrl.getStrResponse() //检测书源是否已登录 bookSource.loginCheckJs?.let { checkJs -> if (checkJs.isNotBlank()) { @@ -305,7 +305,6 @@ object WebBook { headerMapF = bookSource.getHeaderMap(true) ) var res = analyzeUrl.getStrResponse( - bookSource.bookSourceUrl, jsStr = bookSource.getContentRule().webJs, sourceRegex = bookSource.getContentRule().sourceRegex )