问题
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