GeckoFX Observer Service

99封情书 提交于 2019-12-23 04:47:55

问题


I'm trying to block specific images on a page from loading, but I've ran into a bit of trouble.

GeckoWebBrowser's HttpActivityObserver is returning a Not Implemented exception and crashing my program, so I'm trying to implement my own observer but the observe method isn't being called.

Any ideas would be helpful.

public class HttpObserver : nsIObserver
{
    private nsIObserverService service;
    private List<string> bans;

    public HttpObserver()
    {
        bans = new List<string>();
        service = Xpcom.CreateInstance<nsIObserverService>("@mozilla.org/observer-service;1");
    }

    public void Register()
    {
        service.AddObserver(this, "http-on-modify-request", false);
    }

    public void Unregister()
    {
        service.RemoveObserver(this, "http-on-modify-request");
    }

    public void BanUrl(string url)
    {
        bans.Add(url);
    }

    public void Observe(nsISupports aSubject, string aTopic, string aData)
    {
        nsIHttpChannel httpChannel = Xpcom.QueryInterface<nsIHttpChannel>(aSubject);

        if (aTopic == "http-on-modify-request")
        {
            foreach (string url in bans)
            {
                if (url == httpChannel.GetURIAttribute().ToUri().AbsoluteUri)
                {
                    httpChannel.Cancel(unchecked((int)0x804b0002));
                }
            }
        }
    }
}

回答1:


Figured it out. For anyone else struggling with this, replace Xpcom.CreateInterface with Xpcom.GetService




回答2:


Thanks, i try to find alternative BeforeNavigate2 and all you need just:

Gecko.GeckoWebBrowser wb = new GeckoWebBrowser { Dock = DockStyle.Fill, UseHttpActivityObserver = true };

wb.ObserveHttpModifyRequest += (o, e) => { MessageBox.Show(e.Uri.ToString()); };


来源:https://stackoverflow.com/questions/24345502/geckofx-observer-service

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!