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.USER_AGENT
import com.lagradost.cloudstream3.app
import com.lagradost.cloudstream3.mvvm.logError
import com.lagradost.cloudstream3.utils.Coroutines.main
import kotlinx.coroutines.delay
import kotlinx.coroutines.runBlocking
@ -60,121 +61,128 @@ class WebViewResolver(val interceptUrl: Regex, val additionalUrls: List<Regex> =
main {
// Useful for debugging
// WebView.setWebContentsDebuggingEnabled(true)
webView = WebView(
AcraApplication.context
?: throw RuntimeException("No base context in WebViewResolver")
).apply {
// Bare minimum to bypass captcha
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = USER_AGENT
// Blocks unnecessary images, remove if captcha fucks.
settings.blockNetworkImage = true
}
try {
webView = WebView(
AcraApplication.context
?: throw RuntimeException("No base context in WebViewResolver")
).apply {
// Bare minimum to bypass captcha
settings.javaScriptEnabled = true
settings.domStorageEnabled = true
settings.userAgentString = USER_AGENT
// Blocks unnecessary images, remove if captcha fucks.
settings.blockNetworkImage = true
}
webView?.webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? = runBlocking {
val webViewUrl = request.url.toString()
webView?.webViewClient = object : WebViewClient() {
override fun shouldInterceptRequest(
view: WebView,
request: WebResourceRequest
): WebResourceResponse? = runBlocking {
val webViewUrl = request.url.toString()
// println("Loading WebView URL: $webViewUrl")
if (interceptUrl.containsMatchIn(webViewUrl)) {
fixedRequest = request.toRequest().also {
if (requestCallBack(it)) destroyWebView()
if (interceptUrl.containsMatchIn(webViewUrl)) {
fixedRequest = request.toRequest().also {
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) }) {
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
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
handler?.proceed() // Ignore ssl issues
}
}
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
handler?.proceed() // Ignore ssl issues
}
webView?.loadUrl(url, headers.toMap())
} catch (e: Exception) {
logError(e)
}
webView?.loadUrl(url, headers.toMap())
}
var loop = 0

View File

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

View File

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