GeckoView 中是否有 shouldOverrideUrlLoading?

eli*_*m98 3 java firefox android gecko geckoview

我非常熟悉在Android WebView 中使用shouldOverrideUrlLoading 方法,并在几个项目中使用过它。我有一个需要 Mozilla 的 GeckoView 而不是标准 WebView 的新项目,但我似乎无法找到覆盖 url 的方法(以防止用户从最初加载的网站上点击某些链接)。有没有这样的方法?

我已经按照以下说明将 GeckoView 嵌入到我的项目中:https ://wiki.mozilla.org/Mobile/GeckoView并且网站渲染得很好。

我试图模拟的 Android WebView 代码如下所示:

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;
  }
});
Run Code Online (Sandbox Code Playgroud)

GeckoView 中是否有类似的方法?

Art*_*jia 6

我认为你正在寻找的是在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()
}
Run Code Online (Sandbox Code Playgroud)

如果您有任何其他问题,您也可以在他们的GitHub 存储库上打开一个问题。您可能感兴趣的另一个项目是Mozilla Android Components