Search code examples
androidcordovainappbrowser

Cordova intercept URL loading of inappbrowser


I would like to know how to intercept URL loading from inside the WebView in Cordova InAppBrowser.

Currently, I can intercept URL loading from the main WebView with the following code in my MainActivity:

   final WebView myWebView = (WebView) this.appView.getView();
    myWebView.setWebViewClient(new SystemWebViewClient((SystemWebViewEngine) this.appView.getEngine()) {

      @Override
      public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
        // Some custom logic
      }

But when I load some URL inside InAppBrowser Webview, this code does not intercept that request. Any suggestions?


Solution

  • You'll have to modify the inAppBrowser code

    https://github.com/apache/cordova-plugin-inappbrowser/blob/master/src/android/InAppBrowser.java

    On line 646 the webViewClient is set:

    inAppWebView.setWebViewClient(client);
    

    client has the InAppBrowserClient class, it's defined on line 742. You can add the shouldInterceptRequest logic there or use your own WebViewClient subclass