|
|
|
@ -4,18 +4,24 @@ import android.annotation.SuppressLint
|
|
|
|
import android.content.Context
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.Base64
|
|
|
|
import android.util.Base64
|
|
|
|
import android.webkit.WebView
|
|
|
|
import android.webkit.WebView
|
|
|
|
|
|
|
|
import androidx.annotation.MainThread
|
|
|
|
import androidx.core.os.LocaleListCompat
|
|
|
|
import androidx.core.os.LocaleListCompat
|
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
|
|
import dagger.hilt.android.qualifiers.ApplicationContext
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
|
|
|
import kotlinx.coroutines.runBlocking
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import kotlinx.coroutines.withContext
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import okhttp3.OkHttpClient
|
|
|
|
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
|
|
|
import org.koitharu.kotatsu.core.network.MangaHttpClient
|
|
|
|
import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar
|
|
|
|
import org.koitharu.kotatsu.core.network.cookies.MutableCookieJar
|
|
|
|
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
|
|
|
import org.koitharu.kotatsu.core.prefs.SourceSettings
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.configureForParser
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.printStackTraceDebug
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.toList
|
|
|
|
import org.koitharu.kotatsu.core.util.ext.toList
|
|
|
|
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
|
|
|
import org.koitharu.kotatsu.parsers.MangaLoaderContext
|
|
|
|
import org.koitharu.kotatsu.parsers.config.MangaSourceConfig
|
|
|
|
import org.koitharu.kotatsu.parsers.config.MangaSourceConfig
|
|
|
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
|
|
import org.koitharu.kotatsu.parsers.model.MangaSource
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.parsers.network.UserAgents
|
|
|
|
|
|
|
|
import org.koitharu.kotatsu.parsers.util.SuspendLazy
|
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
import java.lang.ref.WeakReference
|
|
|
|
import java.util.Locale
|
|
|
|
import java.util.Locale
|
|
|
|
import javax.inject.Inject
|
|
|
|
import javax.inject.Inject
|
|
|
|
@ -32,12 +38,15 @@ class MangaLoaderContextImpl @Inject constructor(
|
|
|
|
|
|
|
|
|
|
|
|
private var webViewCached: WeakReference<WebView>? = null
|
|
|
|
private var webViewCached: WeakReference<WebView>? = null
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private val userAgentLazy = SuspendLazy {
|
|
|
|
|
|
|
|
withContext(Dispatchers.Main) {
|
|
|
|
|
|
|
|
obtainWebView().settings.userAgentString
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
|
|
@SuppressLint("SetJavaScriptEnabled")
|
|
|
|
override suspend fun evaluateJs(script: String): String? = withContext(Dispatchers.Main) {
|
|
|
|
override suspend fun evaluateJs(script: String): String? = withContext(Dispatchers.Main) {
|
|
|
|
val webView = webViewCached?.get() ?: WebView(androidContext).also {
|
|
|
|
val webView = obtainWebView()
|
|
|
|
it.settings.javaScriptEnabled = true
|
|
|
|
|
|
|
|
webViewCached = WeakReference(it)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
suspendCoroutine { cont ->
|
|
|
|
suspendCoroutine { cont ->
|
|
|
|
webView.evaluateJavascript(script) { result ->
|
|
|
|
webView.evaluateJavascript(script) { result ->
|
|
|
|
cont.resume(result?.takeUnless { it == "null" })
|
|
|
|
cont.resume(result?.takeUnless { it == "null" })
|
|
|
|
@ -45,6 +54,14 @@ class MangaLoaderContextImpl @Inject constructor(
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
override fun getDefaultUserAgent(): String = runCatching {
|
|
|
|
|
|
|
|
runBlocking {
|
|
|
|
|
|
|
|
userAgentLazy.get()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}.onFailure { e ->
|
|
|
|
|
|
|
|
e.printStackTraceDebug()
|
|
|
|
|
|
|
|
}.getOrDefault(UserAgents.FIREFOX_MOBILE)
|
|
|
|
|
|
|
|
|
|
|
|
override fun getConfig(source: MangaSource): MangaSourceConfig {
|
|
|
|
override fun getConfig(source: MangaSource): MangaSourceConfig {
|
|
|
|
return SourceSettings(androidContext, source)
|
|
|
|
return SourceSettings(androidContext, source)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -60,4 +77,12 @@ class MangaLoaderContextImpl @Inject constructor(
|
|
|
|
override fun getPreferredLocales(): List<Locale> {
|
|
|
|
override fun getPreferredLocales(): List<Locale> {
|
|
|
|
return LocaleListCompat.getAdjustedDefault().toList()
|
|
|
|
return LocaleListCompat.getAdjustedDefault().toList()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@MainThread
|
|
|
|
|
|
|
|
private fun obtainWebView(): WebView {
|
|
|
|
|
|
|
|
return webViewCached?.get() ?: WebView(androidContext).also {
|
|
|
|
|
|
|
|
it.configureForParser(null)
|
|
|
|
|
|
|
|
webViewCached = WeakReference(it)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|