Happymh: revert to headers custom UA (#1394)
Partially reverts 1eb9f2bc13242c38e99362c198e84133261d1135
This commit is contained in:
parent
fc000a5058
commit
fdc57d60a4
@ -1,11 +1,7 @@
|
||||
ext {
|
||||
extName = 'Happymh'
|
||||
extClass = '.Happymh'
|
||||
extVersionCode = 7
|
||||
extVersionCode = 8
|
||||
}
|
||||
|
||||
apply from: "$rootDir/common.gradle"
|
||||
|
||||
dependencies {
|
||||
implementation(project(":lib:randomua"))
|
||||
}
|
||||
|
@ -1,15 +1,12 @@
|
||||
package eu.kanade.tachiyomi.extension.zh.happymh
|
||||
|
||||
import android.app.Application
|
||||
import android.widget.Toast
|
||||
import androidx.preference.EditTextPreference
|
||||
import androidx.preference.PreferenceScreen
|
||||
import eu.kanade.tachiyomi.extension.zh.happymh.dto.ChapterListDto
|
||||
import eu.kanade.tachiyomi.extension.zh.happymh.dto.PageListResponseDto
|
||||
import eu.kanade.tachiyomi.extension.zh.happymh.dto.PopularResponseDto
|
||||
import eu.kanade.tachiyomi.lib.randomua.PREF_KEY_CUSTOM_UA
|
||||
import eu.kanade.tachiyomi.lib.randomua.addRandomUAPreferenceToScreen
|
||||
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.network.GET
|
||||
import eu.kanade.tachiyomi.network.POST
|
||||
import eu.kanade.tachiyomi.source.ConfigurableSource
|
||||
@ -24,6 +21,7 @@ import kotlinx.serialization.decodeFromString
|
||||
import kotlinx.serialization.json.Json
|
||||
import kotlinx.serialization.json.decodeFromStream
|
||||
import okhttp3.FormBody
|
||||
import okhttp3.Headers
|
||||
import okhttp3.OkHttpClient
|
||||
import okhttp3.Request
|
||||
import okhttp3.Response
|
||||
@ -31,25 +29,35 @@ import uy.kohesive.injekt.Injekt
|
||||
import uy.kohesive.injekt.api.get
|
||||
import uy.kohesive.injekt.injectLazy
|
||||
|
||||
const val PREF_KEY_CUSTOM_UA = "pref_key_custom_ua_"
|
||||
|
||||
class Happymh : HttpSource(), ConfigurableSource {
|
||||
override val name: String = "嗨皮漫画"
|
||||
override val lang: String = "zh"
|
||||
override val supportsLatest: Boolean = true
|
||||
override val baseUrl: String = "https://m.happymh.com"
|
||||
override val client: OkHttpClient
|
||||
override val client: OkHttpClient = network.cloudflareClient
|
||||
private val json: Json by injectLazy()
|
||||
|
||||
private val preferences = Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
||||
|
||||
init {
|
||||
val preferences = Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
||||
val oldUa = preferences.getString("userAgent", null)
|
||||
if (oldUa != null) {
|
||||
val editor = preferences.edit().remove("userAgent")
|
||||
if (oldUa.isNotBlank()) editor.putString(PREF_KEY_CUSTOM_UA, oldUa)
|
||||
editor.apply()
|
||||
}
|
||||
client = network.cloudflareClient.newBuilder()
|
||||
.setRandomUserAgent(preferences.getPrefUAType(), preferences.getPrefCustomUA())
|
||||
.build()
|
||||
}
|
||||
|
||||
override fun headersBuilder(): Headers.Builder {
|
||||
val builder = super.headersBuilder()
|
||||
val userAgent = preferences.getString(PREF_KEY_CUSTOM_UA, "")!!
|
||||
return if (userAgent.isNotBlank()) {
|
||||
builder.set("User-Agent", userAgent)
|
||||
} else {
|
||||
builder
|
||||
}
|
||||
}
|
||||
|
||||
// Popular
|
||||
@ -147,7 +155,23 @@ class Happymh : HttpSource(), ConfigurableSource {
|
||||
override fun imageUrlParse(response: Response): String = throw UnsupportedOperationException()
|
||||
|
||||
override fun setupPreferenceScreen(screen: PreferenceScreen) {
|
||||
addRandomUAPreferenceToScreen(screen)
|
||||
val context = screen.context
|
||||
|
||||
EditTextPreference(context).apply {
|
||||
key = PREF_KEY_CUSTOM_UA
|
||||
title = "User Agent"
|
||||
summary = "留空则使用应用设置中的默认 User Agent,重启生效"
|
||||
|
||||
setOnPreferenceChangeListener { _, newValue ->
|
||||
try {
|
||||
Headers.headersOf("User-Agent", newValue as String)
|
||||
true
|
||||
} catch (e: Throwable) {
|
||||
Toast.makeText(context, "User Agent 无效:${e.message}", Toast.LENGTH_LONG).show()
|
||||
false
|
||||
}
|
||||
}
|
||||
}.let(screen::addPreference)
|
||||
}
|
||||
|
||||
private inline fun <reified T> Response.parseAs(): T = use {
|
||||
|
Loading…
x
Reference in New Issue
Block a user