【问题标题】:How to get current page URL with gecko?如何使用壁虎获取当前页面 URL?
【发布时间】:2020-01-18 14:13:40
【问题描述】:

我正在尝试让我的 android 网络浏览器只打开特定的 url。因此,我想检查加载的 url 是否符合要求,并根据该要求做一些事情。我看到了很多关于 WebView 的答案,但由于我必须使用开源浏览器(Mozilla Firefox),所以我使用的是 gecko。这是我的代码,我试图用 onLoadRequest 做一些事情,但我不知道如何让它工作。谢谢。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    GeckoView view = findViewById(R.id.geckoView);
    GeckoSession session = new GeckoSession();
    GeckoRuntime runtime = GeckoRuntime.create(this);

    session.open(runtime);
    view.setSession(session);
    session.loadUri("https://www.google.com");

    GeckoSession.NavigationDelegate.LoadRequest loadRequest=new GeckoSession.NavigationDelegate.LoadRequest();

   session.getNavigationDelegate().onLoadRequest(session,loadRequest);


}

@Override
public void onLoadRequest(GeckoSession session, GeckoSession.NavigationDelegate.LoadRequest request)
{
    if(request.uri.contains("mail"))
        GeckoResult.fromValue(AllowOrDeny.ALLOW);
    else
        GeckoResult.fromValue(AllowOrDeny.DENY);
}

【问题讨论】:

    标签: java android-studio firefox-addon-sdk gecko geckoview


    【解决方案1】:

    GeckoView 严重依赖其委托来允许针对应用程序处理最相关的机制和事件。

    简而言之,有运行时委托和会话委托,分别设置在 GeckoRuntime 和 GeckoSession 上。 一般模式是,对于每个委托,都有一个 set{DelegateName}Delegate() 方法将委托附加到运行时或会话,但有一个例外是 RuntimeTelemetry.Delegate,它是在 GeckoRuntimeSettings 中设置的。

    委托方法由GeckoView 调用,不应由应用调用。

    在您的情况下,您希望实现 NavigationDelegate 并将您的实现设置在 GeckoSession 上以覆盖默认的顶级页面加载行为。

    class MyNavigationDelegate implements GeckoSession.NavigationDelegate {
        @Override
        public GeckoResult<AllowOrDeny> onLoadRequest(
                final GeckoSession session,
                final LoadRequest request) {
            // TODO: deny/allow based on your constrains.
        }
    
        // TODO: You should implement the rest of the delegate to handle page load
        // errors and new session requests triggered by new-tab/window requests.
    }
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        GeckoView view = findViewById(R.id.geckoView);
        GeckoSession session = new GeckoSession();
        GeckoRuntime runtime = GeckoRuntime.create(this);
    
        session.setNavigationDelegate(new MyNavigationDelegate());
    
        session.open(runtime);
        view.setSession(session);
        session.loadUri("https://www.google.com");
    }
    

    更多细节请参考API reference和GeckoView Example实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 1970-01-01
      • 2012-05-17
      • 1970-01-01
      相关资源
      最近更新 更多