From b58f4977d03c61dfb70dd3344ebd6ccf68aa15cb Mon Sep 17 00:00:00 2001 From: Daniel Date: Mon, 20 Mar 2023 00:08:56 +0200 Subject: [PATCH] =?UTF-8?q?MadTheme:=20Support=20fetching=20pages=20from?= =?UTF-8?q?=20lazy-loading=20hosts=20(like=20MangaB=E2=80=A6=20(#15758)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit MadTheme: Support fetching pages from lazy-loading hosts (like MangaBuddy now) --- .../tachiyomi/multisrc/madtheme/MadTheme.kt | 35 +++++++++++++++++-- .../multisrc/madtheme/MadThemeGenerator.kt | 2 +- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadTheme.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadTheme.kt index 4a3945260..7a778944d 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadTheme.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadTheme.kt @@ -214,8 +214,39 @@ abstract class MadTheme( val html = document.html() if (!html.contains("var mainServer = \"")) { - // No fancy CDN, all images are available directly in tags - return document.select("#chapter-images img").mapIndexed { index, element -> + val chapterImagesFromHtml = document.select("#chapter-images img") + + // 17/03/2023: Certain hosts only embed two pages in their "#chapter-images" and leave + // the rest to be lazily(?) loaded by javascript. Let's extract `chapImages` and compare + // the count against our select query. If both counts are the same, extract the original + // images directly from the tags otherwise pick the higher count. (heuristic) + // First things first, let's verify `chapImages` actually exists. + if (html.contains("var chapImages = '")) { + val chapterImagesFromJs = html + .substringAfter("var chapImages = '") + .substringBefore("'") + .split(',') + + // Make sure chapter images we've got from javascript all have a host, otherwise + // we've got no choice but to fallback to chapter images from HTML. + // TODO: This might need to be solved one day ^ + if (chapterImagesFromJs.all { e -> + e.startsWith("http://") || e.startsWith("https://") + } + ) { + // Great, we can use these. + if (chapterImagesFromHtml.count() < chapterImagesFromJs.count()) { + // Seems like we've hit such a host, let's use the images we've obtained + // from the javascript string. + return chapterImagesFromJs.mapIndexed { index, path -> + Page(index, imageUrl = path) + } + } + } + } + + // No fancy CDN, all images are available directly in tags (hopefully) + return chapterImagesFromHtml.mapIndexed { index, element -> Page(index, imageUrl = element.attr("abs:data-src")) } } diff --git a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadThemeGenerator.kt b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadThemeGenerator.kt index 0ea507da2..2bddeed74 100644 --- a/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadThemeGenerator.kt +++ b/multisrc/src/main/java/eu/kanade/tachiyomi/multisrc/madtheme/MadThemeGenerator.kt @@ -9,7 +9,7 @@ class MadThemeGenerator : ThemeSourceGenerator { override val themeClass = "MadTheme" - override val baseVersionCode: Int = 11 + override val baseVersionCode: Int = 12 override val sources = listOf( SingleLang("BeeHentai", "https://beehentai.com", "en", isNsfw = true),