From bdef6524e7d78d27ec58607b676ea88470b51e09 Mon Sep 17 00:00:00 2001 From: coxju <118901131+coxju@users.noreply.github.com> Date: Sat, 20 Jan 2024 01:08:37 +0530 Subject: [PATCH] feat : run custom js in webviewresolver (#888) Co-authored-by: coxju --- .../cloudstream3/network/WebViewResolver.kt | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt b/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt index 9171aed9..2c11bcdd 100644 --- a/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt +++ b/app/src/main/java/com/lagradost/cloudstream3/network/WebViewResolver.kt @@ -2,6 +2,8 @@ package com.lagradost.cloudstream3.network import android.annotation.SuppressLint import android.net.http.SslError +import android.os.Handler +import android.os.Looper import android.webkit.* import com.lagradost.cloudstream3.AcraApplication 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 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 script pass custom js to execute + * @param scriptCallback will be called with the result from custom js * */ class WebViewResolver( val interceptUrl: Regex, val additionalUrls: List = emptyList(), val userAgent: String? = USER_AGENT, - val useOkhttp: Boolean = true + val useOkhttp: Boolean = true, + val script: String? = null, + val scriptCallback: ((String) -> Unit)? = null ) : Interceptor { + constructor( + interceptUrl: Regex, + additionalUrls: List = emptyList(), + userAgent: String? = USER_AGENT, + useOkhttp: Boolean = true + ) : this(interceptUrl, additionalUrls, userAgent, useOkhttp, null, null) + companion object { var webViewUserAgent: String? = null @@ -136,6 +149,14 @@ class WebViewResolver( val webViewUrl = request.url.toString() 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)) { fixedRequest = request.toRequest()?.also { requestCallBack(it)