From d07fbd5294c8184adedd60c1b7822088e37dfabf Mon Sep 17 00:00:00 2001 From: Chopper <156493704+choppeh@users.noreply.github.com> Date: Mon, 29 Jul 2024 00:00:57 -0300 Subject: [PATCH] TsukiMangas: Fix (#4295) * Fix content loading for the logged-in user * Some changes * Remove annotation * Remove unneeded code * Fix indentation * Add suppress lint * Enable blockNetworkImage Co-authored-by: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> --------- Co-authored-by: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> --- src/pt/tsukimangas/build.gradle | 2 +- .../extension/pt/tsukimangas/TsukiMangas.kt | 59 ++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/src/pt/tsukimangas/build.gradle b/src/pt/tsukimangas/build.gradle index 648d31133..1118b1bc2 100644 --- a/src/pt/tsukimangas/build.gradle +++ b/src/pt/tsukimangas/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'Tsuki Mangás' extClass = '.TsukiMangas' - extVersionCode = 5 + extVersionCode = 6 isNsfw = true } diff --git a/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt b/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt index c9c2b1031..77631b1ff 100644 --- a/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt +++ b/src/pt/tsukimangas/src/eu/kanade/tachiyomi/extension/pt/tsukimangas/TsukiMangas.kt @@ -1,5 +1,11 @@ package eu.kanade.tachiyomi.extension.pt.tsukimangas +import android.annotation.SuppressLint +import android.app.Application +import android.os.Handler +import android.os.Looper +import android.webkit.WebView +import android.webkit.WebViewClient import eu.kanade.tachiyomi.extension.pt.tsukimangas.dto.ChapterListDto import eu.kanade.tachiyomi.extension.pt.tsukimangas.dto.CompleteMangaDto import eu.kanade.tachiyomi.extension.pt.tsukimangas.dto.MangaListDto @@ -22,9 +28,13 @@ import okhttp3.Interceptor import okhttp3.Request import okhttp3.Response import rx.Observable +import uy.kohesive.injekt.Injekt +import uy.kohesive.injekt.api.get import uy.kohesive.injekt.injectLazy import java.text.SimpleDateFormat import java.util.Locale +import java.util.concurrent.CountDownLatch +import java.util.concurrent.TimeUnit class TsukiMangas : HttpSource() { @@ -38,6 +48,41 @@ class TsukiMangas : HttpSource() { override val supportsLatest = true + @delegate:SuppressLint("SetJavaScriptEnabled") + private val token: String by lazy { + val latch = CountDownLatch(1) + var token = "" + Handler(Looper.getMainLooper()).post { + val webView = WebView(Injekt.get()) + with(webView.settings) { + javaScriptEnabled = true + domStorageEnabled = true + databaseEnabled = true + blockNetworkImage = true + } + webView.webViewClient = object : WebViewClient() { + override fun onPageFinished(view: WebView, url: String) { + val script = "javascript:localStorage['token']" + view.evaluateJavascript(script) { + view.apply { + stopLoading() + destroy() + } + if (it.isBlank() || it in listOf("null", "undefined")) { + return@evaluateJavascript + } + token = it.replace("[\"]+".toRegex(), "") + latch.countDown() + } + } + } + webView.loadUrl(baseUrl) + } + latch.await(10, TimeUnit.SECONDS) + + token + } + override val client by lazy { network.client.newBuilder() .addInterceptor(::apiHeadersInterceptor) @@ -213,7 +258,16 @@ class TsukiMangas : HttpSource() { // ============================= Utilities ============================== private inline fun Response.parseAs(): T = use { - json.decodeFromStream(it.body.byteStream()) + try { + json.decodeFromStream(it.body.byteStream()) + } catch (_: Exception) { + throw Exception( + """ + Contéudo protegido ou foi removido. + Faça o login na WebView e tente novamente + """.trimIndent(), + ) + } } private fun HttpUrl.Builder.addIfNotBlank(query: String, value: String): HttpUrl.Builder { @@ -284,6 +338,9 @@ class TsukiMangas : HttpSource() { val newRequest = request.newBuilder().apply { apiHeaders.entries.forEach { addHeader(it.key, it.value) } + if (token.isNotBlank()) { + addHeader("Authorization", "Bearer $token") + } }.build() return chain.proceed(newRequest)