Add custom UA to Happymh and Jinman Tiantang (#957)
This commit is contained in:
parent
956656cc3e
commit
46990a4d9f
|
@ -34,7 +34,9 @@ fun SharedPreferences.getPrefCustomUA(): String? {
|
||||||
fun addRandomUAPreferenceToScreen(
|
fun addRandomUAPreferenceToScreen(
|
||||||
screen: PreferenceScreen,
|
screen: PreferenceScreen,
|
||||||
) {
|
) {
|
||||||
ListPreference(screen.context).apply {
|
val context = screen.context
|
||||||
|
|
||||||
|
ListPreference(context).apply {
|
||||||
key = PREF_KEY_RANDOM_UA
|
key = PREF_KEY_RANDOM_UA
|
||||||
title = TITLE_RANDOM_UA
|
title = TITLE_RANDOM_UA
|
||||||
entries = RANDOM_UA_ENTRIES
|
entries = RANDOM_UA_ENTRIES
|
||||||
|
@ -43,28 +45,28 @@ fun addRandomUAPreferenceToScreen(
|
||||||
setDefaultValue("off")
|
setDefaultValue("off")
|
||||||
}.also(screen::addPreference)
|
}.also(screen::addPreference)
|
||||||
|
|
||||||
EditTextPreference(screen.context).apply {
|
EditTextPreference(context).apply {
|
||||||
key = PREF_KEY_CUSTOM_UA
|
key = PREF_KEY_CUSTOM_UA
|
||||||
title = TITLE_CUSTOM_UA
|
title = TITLE_CUSTOM_UA
|
||||||
summary = CUSTOM_UA_SUMMARY
|
summary = CUSTOM_UA_SUMMARY
|
||||||
setOnPreferenceChangeListener { _, newValue ->
|
setOnPreferenceChangeListener { _, newValue ->
|
||||||
try {
|
try {
|
||||||
Headers.Builder().add("User-Agent", newValue as String).build()
|
Headers.headersOf("User-Agent", newValue as String)
|
||||||
true
|
true
|
||||||
} catch (e: IllegalArgumentException) {
|
} 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
|
false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.also(screen::addPreference)
|
}.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_"
|
const val PREF_KEY_RANDOM_UA = "pref_key_random_ua_"
|
||||||
val RANDOM_UA_ENTRIES = arrayOf("OFF", "Desktop", "Mobile")
|
val RANDOM_UA_ENTRIES = arrayOf("OFF", "Desktop", "Mobile")
|
||||||
val RANDOM_UA_VALUES = 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 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)"
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,11 @@
|
||||||
ext {
|
ext {
|
||||||
extName = 'Happymh'
|
extName = 'Happymh'
|
||||||
extClass = '.Happymh'
|
extClass = '.Happymh'
|
||||||
extVersionCode = 6
|
extVersionCode = 7
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$rootDir/common.gradle"
|
apply from: "$rootDir/common.gradle"
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(project(":lib:randomua"))
|
||||||
|
}
|
||||||
|
|
|
@ -1,12 +1,15 @@
|
||||||
package eu.kanade.tachiyomi.extension.zh.happymh
|
package eu.kanade.tachiyomi.extension.zh.happymh
|
||||||
|
|
||||||
import android.app.Application
|
import android.app.Application
|
||||||
import android.widget.Toast
|
|
||||||
import androidx.preference.EditTextPreference
|
|
||||||
import androidx.preference.PreferenceScreen
|
import androidx.preference.PreferenceScreen
|
||||||
import eu.kanade.tachiyomi.extension.zh.happymh.dto.ChapterListDto
|
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.PageListResponseDto
|
||||||
import eu.kanade.tachiyomi.extension.zh.happymh.dto.PopularResponseDto
|
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.GET
|
||||||
import eu.kanade.tachiyomi.network.POST
|
import eu.kanade.tachiyomi.network.POST
|
||||||
import eu.kanade.tachiyomi.source.ConfigurableSource
|
import eu.kanade.tachiyomi.source.ConfigurableSource
|
||||||
|
@ -21,7 +24,6 @@ import kotlinx.serialization.decodeFromString
|
||||||
import kotlinx.serialization.json.Json
|
import kotlinx.serialization.json.Json
|
||||||
import kotlinx.serialization.json.decodeFromStream
|
import kotlinx.serialization.json.decodeFromStream
|
||||||
import okhttp3.FormBody
|
import okhttp3.FormBody
|
||||||
import okhttp3.Headers
|
|
||||||
import okhttp3.OkHttpClient
|
import okhttp3.OkHttpClient
|
||||||
import okhttp3.Request
|
import okhttp3.Request
|
||||||
import okhttp3.Response
|
import okhttp3.Response
|
||||||
|
@ -34,21 +36,20 @@ class Happymh : HttpSource(), ConfigurableSource {
|
||||||
override val lang: String = "zh"
|
override val lang: String = "zh"
|
||||||
override val supportsLatest: Boolean = true
|
override val supportsLatest: Boolean = true
|
||||||
override val baseUrl: String = "https://m.happymh.com"
|
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 json: Json by injectLazy()
|
||||||
|
|
||||||
private val preferences by lazy {
|
init {
|
||||||
Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
val preferences = Injekt.get<Application>().getSharedPreferences("source_$id", 0x0000)
|
||||||
}
|
val oldUa = preferences.getString("userAgent", null)
|
||||||
|
if (oldUa != null) {
|
||||||
override fun headersBuilder(): Headers.Builder {
|
val editor = preferences.edit().remove("userAgent")
|
||||||
val builder = super.headersBuilder()
|
if (oldUa.isNotBlank()) editor.putString(PREF_KEY_CUSTOM_UA, oldUa)
|
||||||
val userAgent = preferences.getString(USER_AGENT_PREF, "")!!
|
editor.apply()
|
||||||
return if (userAgent.isNotBlank()) {
|
|
||||||
builder.set("User-Agent", userAgent)
|
|
||||||
} else {
|
|
||||||
builder
|
|
||||||
}
|
}
|
||||||
|
client = network.cloudflareClient.newBuilder()
|
||||||
|
.setRandomUserAgent(preferences.getPrefUAType(), preferences.getPrefCustomUA())
|
||||||
|
.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Popular
|
// Popular
|
||||||
|
@ -146,30 +147,10 @@ class Happymh : HttpSource(), ConfigurableSource {
|
||||||
override fun imageUrlParse(response: Response): String = throw UnsupportedOperationException()
|
override fun imageUrlParse(response: Response): String = throw UnsupportedOperationException()
|
||||||
|
|
||||||
override fun setupPreferenceScreen(screen: PreferenceScreen) {
|
override fun setupPreferenceScreen(screen: PreferenceScreen) {
|
||||||
val context = screen.context
|
addRandomUAPreferenceToScreen(screen)
|
||||||
|
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private inline fun <reified T> Response.parseAs(): T = use {
|
private inline fun <reified T> Response.parseAs(): T = use {
|
||||||
json.decodeFromStream(it.body.byteStream())
|
json.decodeFromStream(it.body.byteStream())
|
||||||
}
|
}
|
||||||
|
|
||||||
companion object {
|
|
||||||
private const val USER_AGENT_PREF = "userAgent"
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,12 @@
|
||||||
ext {
|
ext {
|
||||||
extName = 'Jinman Tiantang'
|
extName = 'Jinman Tiantang'
|
||||||
extClass = '.Jinmantiantang'
|
extClass = '.Jinmantiantang'
|
||||||
extVersionCode = 40
|
extVersionCode = 41
|
||||||
isNsfw = true
|
isNsfw = true
|
||||||
}
|
}
|
||||||
|
|
||||||
apply from: "$rootDir/common.gradle"
|
apply from: "$rootDir/common.gradle"
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
implementation(project(":lib:randomua"))
|
||||||
|
}
|
||||||
|
|
|
@ -2,6 +2,10 @@ package eu.kanade.tachiyomi.extension.zh.jinmantiantang
|
||||||
|
|
||||||
import android.content.SharedPreferences
|
import android.content.SharedPreferences
|
||||||
import androidx.preference.PreferenceScreen
|
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.GET
|
||||||
import eu.kanade.tachiyomi.network.asObservableSuccess
|
import eu.kanade.tachiyomi.network.asObservableSuccess
|
||||||
import eu.kanade.tachiyomi.network.interceptor.rateLimitHost
|
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_PREF, MAINSITE_RATELIMIT_PREF_DEFAULT)!!.toInt(),
|
||||||
preferences.getString(MAINSITE_RATELIMIT_PERIOD, MAINSITE_RATELIMIT_PERIOD_DEFAULT)!!.toLong(),
|
preferences.getString(MAINSITE_RATELIMIT_PERIOD, MAINSITE_RATELIMIT_PERIOD_DEFAULT)!!.toLong(),
|
||||||
)
|
)
|
||||||
|
.setRandomUserAgent(preferences.getPrefUAType(), preferences.getPrefCustomUA())
|
||||||
.apply { interceptors().add(0, updateUrlInterceptor) }
|
.apply { interceptors().add(0, updateUrlInterceptor) }
|
||||||
.addInterceptor(ScrambledImageInterceptor).build()
|
.addInterceptor(ScrambledImageInterceptor).build()
|
||||||
|
|
||||||
|
@ -383,6 +388,7 @@ class Jinmantiantang : ParsedHttpSource(), ConfigurableSource {
|
||||||
|
|
||||||
override fun setupPreferenceScreen(screen: PreferenceScreen) {
|
override fun setupPreferenceScreen(screen: PreferenceScreen) {
|
||||||
getPreferenceList(screen.context, preferences, updateUrlInterceptor.isUpdated).forEach(screen::addPreference)
|
getPreferenceList(screen.context, preferences, updateUrlInterceptor.isUpdated).forEach(screen::addPreference)
|
||||||
|
addRandomUAPreferenceToScreen(screen)
|
||||||
}
|
}
|
||||||
companion object {
|
companion object {
|
||||||
private const val PREFIX_ID_SEARCH_NO_COLON = "JM"
|
private const val PREFIX_ID_SEARCH_NO_COLON = "JM"
|
||||||
|
|
|
@ -84,7 +84,6 @@ private val SITE_ENTRIES_ARRAY_DESCRIPTION get() = arrayOf(
|
||||||
"东南亚线路2",
|
"东南亚线路2",
|
||||||
)
|
)
|
||||||
|
|
||||||
// List is based on https://jmcomic1.bet/
|
|
||||||
// Please also update AndroidManifest
|
// Please also update AndroidManifest
|
||||||
private val SITE_ENTRIES_ARRAY get() = arrayOf(
|
private val SITE_ENTRIES_ARRAY get() = arrayOf(
|
||||||
"18comic.vip",
|
"18comic.vip",
|
||||||
|
@ -93,7 +92,7 @@ private val SITE_ENTRIES_ARRAY get() = arrayOf(
|
||||||
"jmcomic1.me",
|
"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 DEFAULT_LIST_PREF = "defaultBaseUrlList"
|
||||||
private const val URL_LIST_PREF = "baseUrlList"
|
private const val URL_LIST_PREF = "baseUrlList"
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue