Skip to content

Commit

Permalink
feat : run custom js in webviewresolver (recloudstream#888)
Browse files Browse the repository at this point in the history
Co-authored-by: coxju <coxju>
  • Loading branch information
coxju authored Jan 19, 2024
1 parent f40a8d9 commit bdef652
Showing 1 changed file with 22 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<Regex> = 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<Regex> = emptyList(),
userAgent: String? = USER_AGENT,
useOkhttp: Boolean = true
) : this(interceptUrl, additionalUrls, userAgent, useOkhttp, null, null)

companion object {
var webViewUserAgent: String? = null

Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit bdef652

Please sign in to comment.