HiperDex: Update domain (#3688)

* Update domain

* Refactoring of preference URL settings

* Add useLoadMoreRequest
This commit is contained in:
Chopper 2024-06-23 05:23:44 -03:00 committed by Draff
parent d55da9562b
commit 14eefec146
No known key found for this signature in database
GPG Key ID: E8A89F3211677653
2 changed files with 36 additions and 82 deletions

View File

@ -2,8 +2,8 @@ ext {
extName = 'Hiperdex'
extClass = '.Hiperdex'
themePkg = 'madara'
baseUrl = 'https://hiperdex.com'
overrideVersionCode = 12
baseUrl = 'https://hiperdex.top'
overrideVersionCode = 13
isNsfw = true
}

View File

@ -1,7 +1,6 @@
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
@ -10,114 +9,69 @@ import eu.kanade.tachiyomi.lib.randomua.getPrefCustomUA
import eu.kanade.tachiyomi.lib.randomua.getPrefUAType
import eu.kanade.tachiyomi.lib.randomua.setRandomUserAgent
import eu.kanade.tachiyomi.multisrc.madara.Madara
import eu.kanade.tachiyomi.network.interceptor.rateLimit
import eu.kanade.tachiyomi.source.ConfigurableSource
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",
"https://hiperdex.top",
"en",
),
ConfigurableSource {
override val useNewChapterEndpoint: Boolean = true
private val preferences =
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
override val baseUrl by lazy { getPrefBaseUrl() }
private val preferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
override val client = super.client.newBuilder()
.addInterceptor(::domainChangeIntercept)
.setRandomUserAgent(
preferences.getPrefUAType(),
preferences.getPrefCustomUA(),
)
.rateLimit(3)
.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_v2"
private const val BASE_URL_PREF_SUMMARY = "Enter a complete url starting with http"
}
override val useLoadMoreRequest = LoadMoreStrategy.Never
override fun setupPreferenceScreen(screen: PreferenceScreen) {
val baseUrlPref = EditTextPreference(screen.context).apply {
EditTextPreference(screen.context).apply {
key = BASE_URL_PREF
title = BASE_URL_PREF_TITLE
summary = BASE_URL_PREF_SUMMARY
this.setDefaultValue(defaultBaseUrlHost)
dialogTitle = BASE_URL_PREF_TITLE
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
dialogMessage = "Default URL:\n\t${super.baseUrl}"
setDefaultValue(super.baseUrl)
setOnPreferenceChangeListener { _, newValue ->
Toast.makeText(screen.context, RESTART_APP_MESSAGE, Toast.LENGTH_LONG).show()
true
}
}
screen.addPreference(baseUrlPref)
}.also { screen.addPreference(it) }
addRandomUAPreferenceToScreen(screen)
}
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.getString(BASE_URL_PREF, super.baseUrl)!!
private fun getPrefBaseUrl(): String = preferences.baseUrlHost.let { "https://$it" }
init {
preferences.getString(DEFAULT_BASE_URL_PREF, null).let { defaultBaseUrl ->
if (defaultBaseUrl != super.baseUrl) {
preferences.edit()
.putString(BASE_URL_PREF, super.baseUrl)
.putString(DEFAULT_BASE_URL_PREF, super.baseUrl)
.apply()
}
}
}
companion object {
private const val BASE_URL_PREF = "overrideBaseUrl"
private const val BASE_URL_PREF_TITLE = "Edit source URL (requires restart)"
private const val BASE_URL_PREF_SUMMARY = "The default settings will be applied when the extension is next updated"
private const val DEFAULT_BASE_URL_PREF = "defaultBaseUrl"
private const val RESTART_APP_MESSAGE = "Restart app to apply new setting."
}
}