compose-webview-multiplatform icon indicating copy to clipboard operation
compose-webview-multiplatform copied to clipboard

onInterceptUrlRequest not working

Open orelvis15 opened this issue 1 year ago • 8 comments

I was trying to block some urls using onInterceptUrlRequest but when the webview loads the urls it does not activate. I am using the same example from the documentation.

val navigator =
    rememberWebViewNavigator(
        requestInterceptor =
        object : RequestInterceptor {
            override fun onInterceptUrlRequest(
                request: WebRequest,
                navigator: WebViewNavigator,
            ): WebRequestInterceptResult {
                return if (request.url.contains("kotlin")) {
                    WebRequestInterceptResult.Reject
                } else {
                    WebRequestInterceptResult.Allow
                }
            }
        },
    )

orelvis15 avatar Jul 09 '24 16:07 orelvis15

@orelvis15 Could you provide more code?

KevinnZou avatar Jul 17 '24 00:07 KevinnZou

same here, using 1.9.20, here are the code snippets:

class MyRequestInterceptor : RequestInterceptor {

    val lastRequest = mutableStateOf<WebRequest?>(null)

    override fun onInterceptUrlRequest(request: WebRequest, navigator: WebViewNavigator): WebRequestInterceptResult {
        println("intercepting url: ${request.url}")
        lastRequest.value = request
        return WebRequestInterceptResult.Allow
    }
}
...
    val interceptor by remember { mutableStateOf(MyRequestInterceptor()) }
...
    val navigator = rememberWebViewNavigator(requestInterceptor = interceptor)
...
        Text("Last Request: ${interceptor.lastRequest.value?.url}", Modifier.matchParentSize())
...

There is nothing printed and also lastRequest never gets updated. Looks like the intercepter is never injected.

dobe avatar Jul 20 '24 06:07 dobe

jftr: it looks like the first initial request is not intercepted. and also only requests for loading the whole view are intercepted. this made it look like the interceptor is not working at all.

dobe avatar Jul 22 '24 08:07 dobe

jftr: it looks like the first initial request is not intercepted. and also only requests for loading the whole view are intercepted. this made it look like the interceptor is not working at all.

Yes, it is designed to have the same behavior as Android WebView's onInterceptRequest.

KevinnZou avatar Jul 22 '24 14:07 KevinnZou

Same problem here. Request headers is always empty

val navigator = rememberWebViewNavigator(
        requestInterceptor =
        object : RequestInterceptor {
            override fun onInterceptUrlRequest(
                request: WebRequest,
                navigator: WebViewNavigator,
            ): WebRequestInterceptResult {
                Logger.d("URL ${request.url}")
                Logger.d("HEADERS ${request.headers}")
                return WebRequestInterceptResult.Allow
            }
        },
    )

francescogatto avatar Aug 16 '24 16:08 francescogatto

Hello, good afternoon, any updates?

orelvis15 avatar Dec 08 '24 06:12 orelvis15

I find that iOS intercepts the first request, Android does not intercept the first request, and there are some differences

cgpllx avatar Mar 06 '25 02:03 cgpllx