4

I am very familiar with using the shouldOverrideUrlLoading method in Android WebView and have used it in a few projects. I have a new project that requires Mozilla's GeckoView instead of the standard WebView, but I can't seem to find a method to override urls (to prevent a user from following certain links off of the initially-loaded website). Does any method like that exist?

I've embedded GeckoView into my project with these instructions: https://wiki.mozilla.org/Mobile/GeckoView and the websites render great.

The Android WebView code I'm trying to emulate looks like this:

browser.setWebViewClient(new WebViewClient() {
  public boolean shouldOverrideUrlLoading(WebView view, String url) {
    Uri uri = Uri.parse(url);
    if (url.startsWith("https://www.example.com/")) {
      return false;
    }
    return true;
  }
});

Is there any similar method in GeckoView?

jesup
  • 6,765
  • 27
  • 32
elisam98
  • 113
  • 9
  • if it has `setWebViewClient` method, create class that extends `WebViewClient` and override `shouldOverrideUrlLoading` – Cagri Yalcin Apr 30 '19 at 14:27
  • Sorry if I was unclear, @CagriYalcin! The code example was something I pulled from an old project that as using the standard Android WebView. I'm looking for the GeckView way of doing things. – elisam98 Apr 30 '19 at 14:45
  • So check [this](https://github.com/mozilla-mobile/focus-android/issues/2911) out. – Cagri Yalcin May 02 '19 at 07:17

1 Answers1

10

I think what you are looking for is under navigationDelegate#OnLoadRequest

private fun createNavigationDelegate() = object : GeckoSession.NavigationDelegate {
    override fun onLoadRequest(session: GeckoSession, request: GeckoSession.NavigationDelegate.LoadRequest): GeckoResult<AllowOrDeny> {
        return if (request.uri.startsWith("https://www.example.com/")) {
            GeckoResult.fromValue(AllowOrDeny.DENY)
        } else {
            GeckoResult.fromValue(AllowOrDeny.ALLOW)
        }
    }
}

private fun setupGeckoView() {
    geckoView = findViewById(R.id.geckoview)
    val runtime = GeckoRuntime.create(this)
    geckoSession.open(runtime)
    geckoView.setSession(geckoSession)
    geckoSession.loadUri(INITIAL_URL)
    geckoSession.navigationDelegate = createNavigationDelegate()
}

If you have any other questions you can also open an issue on their GitHub repository. Another project that you may be interested in is Mozilla Android Components.

Arturo Mejia
  • 1,862
  • 1
  • 17
  • 25