本文介绍了GeckoFX观察员服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

我正在尝试阻止页面上的特定图像加载,但我遇到了一些麻烦。

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

GeckoWebBrowser的HttpActivityObserver返回未实现异常和崩溃我的程序,所以我试图实现我自己的观察者,但没有调用observe方法。

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));
                }
            }
        }
    }
}


推荐答案

谢谢,我尝试找到替代的BeforeNavigate2以及您所需要的只是:

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()); };

这篇关于GeckoFX观察员服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 19:09