fixed minor crashes

This commit is contained in:
LagradOst 2022-02-18 22:20:35 +01:00
parent 36115b524d
commit d992d63c5f
3 changed files with 135 additions and 119 deletions

View file

@ -6,6 +6,7 @@ import android.webkit.*
import com.lagradost.cloudstream3.AcraApplication import com.lagradost.cloudstream3.AcraApplication
import com.lagradost.cloudstream3.USER_AGENT import com.lagradost.cloudstream3.USER_AGENT
import com.lagradost.cloudstream3.app import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.Coroutines.main import com.lagradost.cloudstream3.utils.Coroutines.main
import kotlinx.coroutines.delay import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
@ -60,121 +61,128 @@ class WebViewResolver(val interceptUrl: Regex, val additionalUrls: List<Regex> =
main { main {
// Useful for debugging // Useful for debugging
// WebView.setWebContentsDebuggingEnabled(true) // WebView.setWebContentsDebuggingEnabled(true)
webView = WebView( try {
AcraApplication.context webView = WebView(
?: throw RuntimeException("No base context in WebViewResolver") AcraApplication.context
).apply { ?: throw RuntimeException("No base context in WebViewResolver")
// Bare minimum to bypass captcha ).apply {
settings.javaScriptEnabled = true // Bare minimum to bypass captcha
settings.domStorageEnabled = true settings.javaScriptEnabled = true
settings.userAgentString = USER_AGENT settings.domStorageEnabled = true
// Blocks unnecessary images, remove if captcha fucks. settings.userAgentString = USER_AGENT
settings.blockNetworkImage = true // Blocks unnecessary images, remove if captcha fucks.
} settings.blockNetworkImage = true
}
webView?.webViewClient = object : WebViewClient() { webView?.webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest( override fun shouldInterceptRequest(
view: WebView, view: WebView,
request: WebResourceRequest request: WebResourceRequest
): WebResourceResponse? = runBlocking { ): WebResourceResponse? = runBlocking {
val webViewUrl = request.url.toString() val webViewUrl = request.url.toString()
// println("Loading WebView URL: $webViewUrl") // println("Loading WebView URL: $webViewUrl")
if (interceptUrl.containsMatchIn(webViewUrl)) { if (interceptUrl.containsMatchIn(webViewUrl)) {
fixedRequest = request.toRequest().also { fixedRequest = request.toRequest().also {
if (requestCallBack(it)) destroyWebView() if (requestCallBack(it)) destroyWebView()
}
println("Web-view request finished: $webViewUrl")
destroyWebView()
return@runBlocking null
}
if (additionalUrls.any { it.containsMatchIn(webViewUrl) }) {
extraRequestList.add(request.toRequest().also {
if (requestCallBack(it)) destroyWebView()
})
}
// Suppress image requests as we don't display them anywhere
// Less data, low chance of causing issues.
// blockNetworkImage also does this job but i will keep it for the future.
val blacklistedFiles = listOf(
".jpg",
".png",
".webp",
".mpg",
".mpeg",
".jpeg",
".webm",
".mp4",
".mp3",
".gifv",
".flv",
".asf",
".mov",
".mng",
".mkv",
".ogg",
".avi",
".wav",
".woff2",
".woff",
".ttf",
".css",
".vtt",
".srt",
".ts",
".gif",
// Warning, this might fuck some future sites, but it's used to make Sflix work.
"wss://"
)
/** NOTE! request.requestHeaders is not perfect!
* They don't contain all the headers the browser actually gives.
* Overriding with okhttp might fuck up otherwise working requests,
* e.g the recaptcha request.
* **/
return@runBlocking try {
when {
blacklistedFiles.any { URI(webViewUrl).path.contains(it) } || webViewUrl.endsWith(
"/favicon.ico"
) -> WebResourceResponse(
"image/png",
null,
null
)
webViewUrl.contains("recaptcha") -> super.shouldInterceptRequest(
view,
request
)
request.method == "GET" -> app.get(
webViewUrl,
headers = request.requestHeaders
).response.toWebResourceResponse()
request.method == "POST" -> app.post(
webViewUrl,
headers = request.requestHeaders
).response.toWebResourceResponse()
else -> return@runBlocking super.shouldInterceptRequest(
view,
request
)
}
} catch (e: Exception) {
null
} }
println("Web-view request finished: $webViewUrl")
destroyWebView()
return@runBlocking null
} }
if (additionalUrls.any { it.containsMatchIn(webViewUrl) }) { override fun onReceivedSslError(
extraRequestList.add(request.toRequest().also { view: WebView?,
if (requestCallBack(it)) destroyWebView() handler: SslErrorHandler?,
}) error: SslError?
} ) {
handler?.proceed() // Ignore ssl issues
// Suppress image requests as we don't display them anywhere
// Less data, low chance of causing issues.
// blockNetworkImage also does this job but i will keep it for the future.
val blacklistedFiles = listOf(
".jpg",
".png",
".webp",
".mpg",
".mpeg",
".jpeg",
".webm",
".mp4",
".mp3",
".gifv",
".flv",
".asf",
".mov",
".mng",
".mkv",
".ogg",
".avi",
".wav",
".woff2",
".woff",
".ttf",
".css",
".vtt",
".srt",
".ts",
".gif",
// Warning, this might fuck some future sites, but it's used to make Sflix work.
"wss://"
)
/** NOTE! request.requestHeaders is not perfect!
* They don't contain all the headers the browser actually gives.
* Overriding with okhttp might fuck up otherwise working requests,
* e.g the recaptcha request.
* **/
return@runBlocking try {
when {
blacklistedFiles.any { URI(webViewUrl).path.contains(it) } || webViewUrl.endsWith(
"/favicon.ico"
) -> WebResourceResponse(
"image/png",
null,
null
)
webViewUrl.contains("recaptcha") -> super.shouldInterceptRequest(
view,
request
)
request.method == "GET" -> app.get(
webViewUrl,
headers = request.requestHeaders
).response.toWebResourceResponse()
request.method == "POST" -> app.post(
webViewUrl,
headers = request.requestHeaders
).response.toWebResourceResponse()
else -> return@runBlocking super.shouldInterceptRequest(view, request)
}
} catch (e: Exception) {
null
} }
} }
webView?.loadUrl(url, headers.toMap())
override fun onReceivedSslError( } catch (e: Exception) {
view: WebView?, logError(e)
handler: SslErrorHandler?,
error: SslError?
) {
handler?.proceed() // Ignore ssl issues
}
} }
webView?.loadUrl(url, headers.toMap())
} }
var loop = 0 var loop = 0

View file

@ -142,19 +142,23 @@ class GeneratorPlayer : FullScreenPlayer() {
} }
private fun openSubPicker() { private fun openSubPicker() {
subsPathPicker.launch( try {
arrayOf( subsPathPicker.launch(
"text/plain", arrayOf(
"text/str", "text/plain",
"application/octet-stream", "text/str",
MimeTypes.TEXT_UNKNOWN, "application/octet-stream",
MimeTypes.TEXT_VTT, MimeTypes.TEXT_UNKNOWN,
MimeTypes.TEXT_SSA, MimeTypes.TEXT_VTT,
MimeTypes.APPLICATION_TTML, MimeTypes.TEXT_SSA,
MimeTypes.APPLICATION_MP4VTT, MimeTypes.APPLICATION_TTML,
MimeTypes.APPLICATION_SUBRIP, MimeTypes.APPLICATION_MP4VTT,
MimeTypes.APPLICATION_SUBRIP,
)
) )
) } catch (e : Exception) {
logError(e)
}
} }
// Open file picker // Open file picker

View file

@ -375,7 +375,11 @@ class SettingsFragment : PreferenceFragmentCompat() {
{}) { {}) {
// Last = custom // Last = custom
if (it == dirs.size) { if (it == dirs.size) {
pathPicker.launch(Uri.EMPTY) try {
pathPicker.launch(Uri.EMPTY)
} catch (e : Exception) {
logError(e)
}
} else { } else {
// Sets both visual and actual paths. // Sets both visual and actual paths.
// key = used path // key = used path