From 509a375d7e9441dc5b680b5cc38993793e0d24a0 Mon Sep 17 00:00:00 2001 From: AwkwardPeak7 <48650614+AwkwardPeak7@users.noreply.github.com> Date: Fri, 6 Sep 2024 16:40:05 +0500 Subject: [PATCH] Mangadex: fix potential anr and remove ua pref (#4935) * Mangadex: fix potential anr and remove ua pref * remove unused stuff * don't remove tachi ua --- src/all/mangadex/build.gradle | 2 +- .../extension/all/mangadex/MDConstants.kt | 7 --- .../extension/all/mangadex/MangaDex.kt | 47 +++---------------- .../all/mangadex/MdUserAgentInterceptor.kt | 39 --------------- 4 files changed, 8 insertions(+), 87 deletions(-) delete mode 100644 src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdUserAgentInterceptor.kt diff --git a/src/all/mangadex/build.gradle b/src/all/mangadex/build.gradle index 668c6349a..00dd57999 100644 --- a/src/all/mangadex/build.gradle +++ b/src/all/mangadex/build.gradle @@ -1,7 +1,7 @@ ext { extName = 'MangaDex' extClass = '.MangaDexFactory' - extVersionCode = 193 + extVersionCode = 194 isNsfw = true } diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt index 8fc471311..69f79d63b 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MDConstants.kt @@ -138,13 +138,6 @@ object MDConstants { return "${altTitlesInDescPref}_$dexLang" } - private const val customUserAgentPref = "customUserAgent" - fun getCustomUserAgentPrefKey(dexLang: String): String { - return "${customUserAgentPref}_$dexLang" - } - - val defaultUserAgent = "Tachiyomi " + System.getProperty("http.agent") - private const val tagGroupContent = "content" private const val tagGroupFormat = "format" private const val tagGroupGenre = "genre" diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt index 29b802d68..17219399d 100644 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt +++ b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MangaDex.kt @@ -3,7 +3,6 @@ package eu.kanade.tachiyomi.extension.all.mangadex import android.app.Application import android.content.SharedPreferences import android.os.Build -import android.widget.Toast import androidx.preference.EditTextPreference import androidx.preference.ListPreference import androidx.preference.MultiSelectListPreference @@ -56,6 +55,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St private val preferences: SharedPreferences by lazy { Injekt.get().getSharedPreferences("source_$id", 0x0000) + .sanitizeExistingUuidPrefs() } private val helper = MangaDexHelper(lang) @@ -67,6 +67,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St "Keiyoushi" val builder = super.headersBuilder().apply { + set("User-Agent", "Tachiyomi " + System.getProperty("http.agent")) set("Referer", "$baseUrl/") set("Origin", baseUrl) set("Extra", extraHeader) @@ -78,13 +79,8 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St override val client = network.client.newBuilder() .rateLimit(3) .addInterceptor(MdAtHomeReportInterceptor(network.client, headers)) - .addInterceptor(MdUserAgentInterceptor(preferences, dexLang)) .build() - init { - preferences.sanitizeExistingUuidPrefs() - } - // Popular manga section override fun popularMangaRequest(page: Int): Request { @@ -395,7 +391,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St // Manga Details section override fun getMangaUrl(manga: SManga): String { - return baseUrl + manga.url + "/" + helper.titleToSlug(manga.title) + return baseUrl + manga.url.replace("/manga/", "/title/") + "/" + helper.titleToSlug(manga.title) } /** @@ -761,30 +757,6 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St } } - val userAgentPref = EditTextPreference(screen.context).apply { - key = MDConstants.getCustomUserAgentPrefKey(dexLang) - title = helper.intl["set_custom_useragent"] - summary = helper.intl["set_custom_useragent_summary"] - dialogMessage = helper.intl.format( - "set_custom_useragent_dialog", - MDConstants.defaultUserAgent, - ) - - setDefaultValue(MDConstants.defaultUserAgent) - - setOnPreferenceChangeListener { _, newValue -> - try { - Headers.Builder().add("User-Agent", newValue as String) - summary = newValue - true - } catch (e: Throwable) { - val errorMessage = helper.intl.format("set_custom_useragent_error_invalid", e.message) - Toast.makeText(screen.context, errorMessage, Toast.LENGTH_LONG).show() - false - } - } - } - screen.addPreference(coverQualityPref) screen.addPreference(tryUsingFirstVolumeCoverPref) screen.addPreference(dataSaverPref) @@ -794,7 +766,6 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St screen.addPreference(originalLanguagePref) screen.addPreference(blockedGroupsPref) screen.addPreference(blockedUploaderPref) - screen.addPreference(userAgentPref) } override fun getFilterList(): FilterList = @@ -869,20 +840,14 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St private val SharedPreferences.altTitlesInDesc get() = getBoolean(MDConstants.getAltTitlesInDescPrefKey(dexLang), false) - private val SharedPreferences.customUserAgent - get() = getString( - MDConstants.getCustomUserAgentPrefKey(dexLang), - MDConstants.defaultUserAgent, - ) - /** * Previous versions of the extension allowed invalid UUID values to be stored in the * preferences. This method clear invalid UUIDs in case the user have updated from * a previous version with that behaviour. */ - private fun SharedPreferences.sanitizeExistingUuidPrefs() { + private fun SharedPreferences.sanitizeExistingUuidPrefs(): SharedPreferences { if (getBoolean(MDConstants.getHasSanitizedUuidsPrefKey(dexLang), false)) { - return + return this } val blockedGroups = getString(MDConstants.getBlockedGroupsPrefKey(dexLang), "")!! @@ -902,5 +867,7 @@ abstract class MangaDex(final override val lang: String, private val dexLang: St .putString(MDConstants.getBlockedUploaderPrefKey(dexLang), blockedUploaders) .putBoolean(MDConstants.getHasSanitizedUuidsPrefKey(dexLang), true) .apply() + + return this } } diff --git a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdUserAgentInterceptor.kt b/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdUserAgentInterceptor.kt deleted file mode 100644 index 451f1ebef..000000000 --- a/src/all/mangadex/src/eu/kanade/tachiyomi/extension/all/mangadex/MdUserAgentInterceptor.kt +++ /dev/null @@ -1,39 +0,0 @@ -package eu.kanade.tachiyomi.extension.all.mangadex - -import android.content.SharedPreferences -import okhttp3.Interceptor -import okhttp3.Response - -/** - * Interceptor to set custom useragent for MangaDex - */ -class MdUserAgentInterceptor( - private val preferences: SharedPreferences, - private val dexLang: String, -) : Interceptor { - - private val SharedPreferences.customUserAgent - get() = getString( - MDConstants.getCustomUserAgentPrefKey(dexLang), - MDConstants.defaultUserAgent, - ) - - override fun intercept(chain: Interceptor.Chain): Response { - val originalRequest = chain.request() - - val newUserAgent = preferences.customUserAgent - ?: return chain.proceed(originalRequest) - - val originalHeaders = originalRequest.headers - - val modifiedHeaders = originalHeaders.newBuilder() - .set("User-Agent", newUserAgent) - .build() - - val modifiedRequest = originalRequest.newBuilder() - .headers(modifiedHeaders) - .build() - - return chain.proceed(modifiedRequest) - } -}