feat : run custom js in webviewresolver (#888)

Co-authored-by: coxju <coxju>
This commit is contained in:
coxju 2024-01-20 01:08:37 +05:30 committed by GitHub
parent f40a8d9418
commit bdef6524e7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,8 @@ package com.lagradost.cloudstream3.network
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.net.http.SslError import android.net.http.SslError
import android.os.Handler
import android.os.Looper
import android.webkit.* import android.webkit.*
import com.lagradost.cloudstream3.AcraApplication import com.lagradost.cloudstream3.AcraApplication
import com.lagradost.cloudstream3.AcraApplication.Companion.context import com.lagradost.cloudstream3.AcraApplication.Companion.context
@ -27,15 +29,26 @@ import java.net.URI
* @param additionalUrls this will make resolveUsingWebView also return all other requests matching the list of Regex. * @param additionalUrls this will make resolveUsingWebView also return all other requests matching the list of Regex.
* @param userAgent if null then will use the default user agent * @param userAgent if null then will use the default user agent
* @param useOkhttp will try to use the okhttp client as much as possible, but this might cause some requests to fail. Disable for cloudflare. * @param useOkhttp will try to use the okhttp client as much as possible, but this might cause some requests to fail. Disable for cloudflare.
* @param script pass custom js to execute
* @param scriptCallback will be called with the result from custom js
* */ * */
class WebViewResolver( class WebViewResolver(
val interceptUrl: Regex, val interceptUrl: Regex,
val additionalUrls: List<Regex> = emptyList(), val additionalUrls: List<Regex> = emptyList(),
val userAgent: String? = USER_AGENT, val userAgent: String? = USER_AGENT,
val useOkhttp: Boolean = true val useOkhttp: Boolean = true,
val script: String? = null,
val scriptCallback: ((String) -> Unit)? = null
) : ) :
Interceptor { Interceptor {
constructor(
interceptUrl: Regex,
additionalUrls: List<Regex> = emptyList(),
userAgent: String? = USER_AGENT,
useOkhttp: Boolean = true
) : this(interceptUrl, additionalUrls, userAgent, useOkhttp, null, null)
companion object { companion object {
var webViewUserAgent: String? = null var webViewUserAgent: String? = null
@ -136,6 +149,14 @@ class WebViewResolver(
val webViewUrl = request.url.toString() val webViewUrl = request.url.toString()
println("Loading WebView URL: $webViewUrl") println("Loading WebView URL: $webViewUrl")
if (script != null) {
val handler = Handler(Looper.getMainLooper())
handler.post {
view.evaluateJavascript("$script")
{ scriptCallback?.invoke(it) }
}
}
if (interceptUrl.containsMatchIn(webViewUrl)) { if (interceptUrl.containsMatchIn(webViewUrl)) {
fixedRequest = request.toRequest()?.also { fixedRequest = request.toRequest()?.also {
requestCallBack(it) requestCallBack(it)