Add custom UA to Happymh and Jinman Tiantang (#957)

This commit is contained in:
stevenyomi 2024-02-04 01:33:57 +08:00 committed by GitHub
parent 64831786ed
commit 1eb9f2bc13
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 43 additions and 47 deletions

View File

@ -34,7 +34,9 @@ fun SharedPreferences.getPrefCustomUA(): String? {
fun addRandomUAPreferenceToScreen(
screen: PreferenceScreen,
) {
ListPreference(screen.context).apply {
val context = screen.context
ListPreference(context).apply {
key = PREF_KEY_RANDOM_UA
title = TITLE_RANDOM_UA
entries = RANDOM_UA_ENTRIES
@ -43,28 +45,28 @@ fun addRandomUAPreferenceToScreen(
setDefaultValue("off")
}.also(screen::addPreference)
EditTextPreference(screen.context).apply {
EditTextPreference(context).apply {
key = PREF_KEY_CUSTOM_UA
title = TITLE_CUSTOM_UA
summary = CUSTOM_UA_SUMMARY
setOnPreferenceChangeListener { _, newValue ->
try {
Headers.Builder().add("User-Agent", newValue as String).build()
Headers.headersOf("User-Agent", newValue as String)
true
} catch (e: IllegalArgumentException) {
Toast.makeText(screen.context, "User Agent invalid${e.message}", Toast.LENGTH_LONG).show()
Toast.makeText(context, "Invalid user agent string: ${e.message}", Toast.LENGTH_LONG).show()
false
}
}
}.also(screen::addPreference)
}
const val TITLE_RANDOM_UA = "Random User-Agent (Requires Restart)"
const val TITLE_RANDOM_UA = "Random user agent string (requires restart)"
const val PREF_KEY_RANDOM_UA = "pref_key_random_ua_"
val RANDOM_UA_ENTRIES = arrayOf("OFF", "Desktop", "Mobile")
val RANDOM_UA_VALUES = arrayOf("off", "desktop", "mobile")
const val TITLE_CUSTOM_UA = "Custom User-Agent (Requires Restart)"
const val TITLE_CUSTOM_UA = "Custom user agent string (requires restart)"
const val PREF_KEY_CUSTOM_UA = "pref_key_custom_ua_"
const val CUSTOM_UA_SUMMARY = "Leave blank to use application default user-agent (IGNORED if Random User-Agent is enabled)"
const val CUSTOM_UA_SUMMARY = "Leave blank to use the default user agent string (ignored if random user agent string is enabled)"

View File

@ -1,7 +1,11 @@
ext {
extName = 'Happymh'
extClass = '.Happymh'
extVersionCode = 6
extVersionCode = 7
}
apply from: "$rootDir/common.gradle"
dependencies {
implementation(project(":lib:randomua"))
}

View File

@ -1,12 +1,15 @@
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
@ -21,7 +24,6 @@ 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
@ -34,21 +36,20 @@ class Happymh : HttpSource(), ConfigurableSource {
override val lang: String = "zh"
override val supportsLatest: Boolean = true
override val baseUrl: String = "https://m.happymh.com"
override val client: OkHttpClient = network.cloudflareClient
override val client: OkHttpClient
private val json: Json by injectLazy()
private val preferences by lazy {
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
}
override fun headersBuilder(): Headers.Builder {
val builder = super.headersBuilder()
val userAgent = preferences.getString(USER_AGENT_PREF, "")!!
return if (userAgent.isNotBlank()) {
builder.set("User-Agent", userAgent)
} else {
builder
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()
}
// Popular
@ -146,30 +147,10 @@ class Happymh : HttpSource(), ConfigurableSource {
override fun imageUrlParse(response: Response): String = throw UnsupportedOperationException()
override fun setupPreferenceScreen(screen: PreferenceScreen) {
val context = screen.context
EditTextPreference(context).apply {
key = USER_AGENT_PREF
title = "User Agent"
summary = "留空则使用应用设置中的默认 User Agent重启生效"
setOnPreferenceChangeListener { _, newValue ->
try {
Headers.Builder().add("User-Agent", newValue as String)
true
} catch (e: Throwable) {
Toast.makeText(context, "User Agent 无效:${e.message}", Toast.LENGTH_LONG).show()
false
}
}
}.let(screen::addPreference)
addRandomUAPreferenceToScreen(screen)
}
private inline fun <reified T> Response.parseAs(): T = use {
json.decodeFromStream(it.body.byteStream())
}
companion object {
private const val USER_AGENT_PREF = "userAgent"
}
}

View File

@ -1,8 +1,12 @@
ext {
extName = 'Jinman Tiantang'
extClass = '.Jinmantiantang'
extVersionCode = 40
extVersionCode = 41
isNsfw = true
}
apply from: "$rootDir/common.gradle"
dependencies {
implementation(project(":lib:randomua"))
}

View File

@ -2,6 +2,10 @@ package eu.kanade.tachiyomi.extension.zh.jinmantiantang
import android.content.SharedPreferences
import androidx.preference.PreferenceScreen
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.asObservableSuccess
import eu.kanade.tachiyomi.network.interceptor.rateLimitHost
@ -47,6 +51,7 @@ class Jinmantiantang : ParsedHttpSource(), ConfigurableSource {
preferences.getString(MAINSITE_RATELIMIT_PREF, MAINSITE_RATELIMIT_PREF_DEFAULT)!!.toInt(),
preferences.getString(MAINSITE_RATELIMIT_PERIOD, MAINSITE_RATELIMIT_PERIOD_DEFAULT)!!.toLong(),
)
.setRandomUserAgent(preferences.getPrefUAType(), preferences.getPrefCustomUA())
.apply { interceptors().add(0, updateUrlInterceptor) }
.addInterceptor(ScrambledImageInterceptor).build()
@ -383,6 +388,7 @@ class Jinmantiantang : ParsedHttpSource(), ConfigurableSource {
override fun setupPreferenceScreen(screen: PreferenceScreen) {
getPreferenceList(screen.context, preferences, updateUrlInterceptor.isUpdated).forEach(screen::addPreference)
addRandomUAPreferenceToScreen(screen)
}
companion object {
private const val PREFIX_ID_SEARCH_NO_COLON = "JM"

View File

@ -84,7 +84,6 @@ private val SITE_ENTRIES_ARRAY_DESCRIPTION get() = arrayOf(
"东南亚线路2",
)
// List is based on https://jmcomic1.bet/
// Please also update AndroidManifest
private val SITE_ENTRIES_ARRAY get() = arrayOf(
"18comic.vip",
@ -93,7 +92,7 @@ private val SITE_ENTRIES_ARRAY get() = arrayOf(
"jmcomic1.me",
)
private const val DEFAULT_LIST = "jm-comic3.art,jm-comic1.art,jm-comic2.ark"
private const val DEFAULT_LIST = "18comic-cn.vip,18comic-c.xyz,18comic-c.art"
private const val DEFAULT_LIST_PREF = "defaultBaseUrlList"
private const val URL_LIST_PREF = "baseUrlList"