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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With