Hiperdex: auto change domain (#17990)

HiperDex: auto change domain
This commit is contained in:
AwkwardPeak7 2023-09-17 01:56:12 +05:00 committed by GitHub
parent 9f23d131df
commit 187c9f17d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 80 additions and 12 deletions

View File

@ -3,41 +3,103 @@ package eu.kanade.tachiyomi.extension.en.hiperdex
import android.app.Application
import android.content.SharedPreferences
import android.widget.Toast
import androidx.preference.EditTextPreference
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.extension.BuildConfig
import eu.kanade.tachiyomi.multisrc.madara.Madara
import okhttp3.HttpUrl.Companion.toHttpUrl
import okhttp3.Interceptor
import okhttp3.Response
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
class Hiperdex : Madara("Hiperdex", "https://hiperdex.com", "en") {
override val useNewChapterEndpoint: Boolean = true
private val defaultBaseUrl = "https://hiperdex.com"
override fun searchPage(page: Int): String {
return if (page > 1) {
"page/$page/"
} else {
""
}
}
override val baseUrl by lazy { getPrefBaseUrl() }
private val preferences: SharedPreferences by lazy {
private val preferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
override val client = super.client.newBuilder()
.addInterceptor(::domainChangeIntercept)
.build()
private var lastDomain = ""
private fun domainChangeIntercept(chain: Interceptor.Chain): Response {
val request = chain.request()
if (request.url.host !in listOf(preferences.baseUrlHost, lastDomain)) {
return chain.proceed(request)
}
if (lastDomain.isNotEmpty()) {
val newUrl = request.url.newBuilder()
.host(preferences.baseUrlHost)
.build()
return chain.proceed(
request.newBuilder()
.url(newUrl)
.build(),
)
}
val response = chain.proceed(request)
if (request.url.host == response.request.url.host) return response
response.close()
preferences.baseUrlHost = response.request.url.host
lastDomain = request.url.host
val newUrl = request.url.newBuilder()
.host(response.request.url.host)
.build()
return chain.proceed(
request.newBuilder()
.url(newUrl)
.build(),
)
}
companion object {
private const val defaultBaseUrlHost = "hiperdex.com"
private const val RESTART_TACHIYOMI = "Restart Tachiyomi to apply new setting."
private const val BASE_URL_PREF_TITLE = "Override BaseUrl"
private const val BASE_URL_PREF = "overrideBaseUrl_v${BuildConfig.VERSION_CODE}"
private const val BASE_URL_PREF_SUMMARY = "For temporary uses. Updating the extension will erase this setting."
private const val BASE_URL_PREF = "overrideBaseUrl_v2"
private const val BASE_URL_PREF_SUMMARY = "Enter a complete url starting with http"
}
override fun setupPreferenceScreen(screen: PreferenceScreen) {
val baseUrlPref = androidx.preference.EditTextPreference(screen.context).apply {
val baseUrlPref = EditTextPreference(screen.context).apply {
key = BASE_URL_PREF
title = BASE_URL_PREF_TITLE
summary = BASE_URL_PREF_SUMMARY
this.setDefaultValue(defaultBaseUrl)
this.setDefaultValue(defaultBaseUrlHost)
dialogTitle = BASE_URL_PREF_TITLE
setOnPreferenceChangeListener { _, _ ->
Toast.makeText(screen.context, RESTART_TACHIYOMI, Toast.LENGTH_LONG).show()
true
setOnPreferenceChangeListener { _, newVal ->
val url = newVal as String
runCatching {
val host = url.toHttpUrl().host
Toast.makeText(screen.context, RESTART_TACHIYOMI, Toast.LENGTH_LONG).show()
preferences.edit().putString(BASE_URL_PREF, host).commit()
}
false
}
}
screen.addPreference(baseUrlPref)
@ -45,5 +107,11 @@ class Hiperdex : Madara("Hiperdex", "https://hiperdex.com", "en") {
super.setupPreferenceScreen(screen)
}
private fun getPrefBaseUrl(): String = preferences.getString(BASE_URL_PREF, defaultBaseUrl)!!
private var SharedPreferences.baseUrlHost
get() = getString(BASE_URL_PREF, defaultBaseUrlHost) ?: defaultBaseUrlHost
set(newHost) {
edit().putString(BASE_URL_PREF, newHost).commit()
}
private fun getPrefBaseUrl(): String = preferences.baseUrlHost.let { "https://$it" }
}

View File

@ -146,7 +146,7 @@ class MadaraGenerator : ThemeSourceGenerator {
SingleLang("HentaiZone", "https://hentaizone.xyz", "fr", isNsfw = true, overrideVersionCode = 1),
SingleLang("HerenScan", "https://herenscan.com", "es"),
SingleLang("HipercooL", "https://hipercool.xyz", "pt-BR", isNsfw = true, className = "Hipercool"),
SingleLang("Hiperdex", "https://hiperdex.com", "en", isNsfw = true, overrideVersionCode = 10),
SingleLang("Hiperdex", "https://hiperdex.com", "en", isNsfw = true, overrideVersionCode = 11),
SingleLang("HistoireDHentai", "https://hhentai.fr", "fr", isNsfw = true),
SingleLang("Hizomanga", "https://hizomanga.com", "ar", overrideVersionCode = 1),
SingleLang("HM2D", "https://mangadistrict.com/hdoujin", "en", isNsfw = true, overrideVersionCode = 1),