Happymh: add setting to override user agent (#15861)

This commit is contained in:
stevenyomi 2023-03-28 21:33:59 +08:00 committed by GitHub
parent aa9ac35a98
commit 5c4ee8e887
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 2 deletions

View File

@ -5,7 +5,7 @@ ext {
extName = 'Happymh'
pkgNameSuffix = 'zh.happymh'
extClass = '.Happymh'
extVersionCode = 2
extVersionCode = 3
}
apply from: "$rootDir/common.gradle"

View File

@ -1,7 +1,11 @@
package eu.kanade.tachiyomi.extension.zh.happymh
import android.app.Application
import androidx.preference.EditTextPreference
import androidx.preference.PreferenceScreen
import eu.kanade.tachiyomi.network.GET
import eu.kanade.tachiyomi.network.POST
import eu.kanade.tachiyomi.source.ConfigurableSource
import eu.kanade.tachiyomi.source.model.FilterList
import eu.kanade.tachiyomi.source.model.MangasPage
import eu.kanade.tachiyomi.source.model.Page
@ -16,12 +20,15 @@ import kotlinx.serialization.json.jsonArray
import kotlinx.serialization.json.jsonObject
import kotlinx.serialization.json.jsonPrimitive
import okhttp3.FormBody
import okhttp3.Headers
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.Response
import uy.kohesive.injekt.Injekt
import uy.kohesive.injekt.api.get
import uy.kohesive.injekt.injectLazy
class Happymh : HttpSource() {
class Happymh : HttpSource(), ConfigurableSource {
override val name: String = "嗨皮漫画"
override val lang: String = "zh"
override val supportsLatest: Boolean = true
@ -29,6 +36,17 @@ class Happymh : HttpSource() {
override val client: OkHttpClient = network.cloudflareClient
private val json: Json by injectLazy()
override fun headersBuilder(): Headers.Builder {
val builder = super.headersBuilder()
val preferences = Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
val userAgent = preferences.getString(USER_AGENT_PREF, "")!!
return if (userAgent.isNotBlank()) {
builder.set("User-Agent", userAgent)
} else {
builder
}
}
// Popular
// Requires login, otherwise result is the same as latest updates
@ -119,4 +137,16 @@ class Happymh : HttpSource() {
}
override fun imageUrlParse(response: Response): String = throw Exception("Not Used")
override fun setupPreferenceScreen(screen: PreferenceScreen) {
EditTextPreference(screen.context).apply {
key = USER_AGENT_PREF
title = "User Agent"
summary = "留空则使用应用设置中的默认 User Agent"
}.let(screen::addPreference)
}
companion object {
private const val USER_AGENT_PREF = "userAgent"
}
}