0

I'm writing an extension for IE in c#. I've got a IWebBrowser2 object called browser and from it I can get an IHtmlDocument2 object like this:

var document = browser.Document as IHTMLDocument2;

Now I need to react on a javascript event which is fired by this code:

var event = document.createEvent("HTMLEvents");
event.initEvent('MyCustomEvent', true, false);
someElement.dispatchEvent(event);

I see that I can use click, mouseleave and other properties from HTMLDocumentEvents2 interface to subscribe to typical events, but I can't find something like OnCustomEvent or HtmlEvents['MyCustomEvent'] += ... , etc.

It was easy to implement this functionality in firefox add-on, but with IE it's a pain... Is this even possible? Thanks

Jim Mischel
  • 131,090
  • 20
  • 188
  • 351
Artur Udod
  • 4,465
  • 1
  • 29
  • 58

2 Answers2

1

You can use attachEvent to listen to the event. I've tested similar code work for onmessage event(doesn't have a specific event handler) in IE 10.

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.AutoDispatch)]
public class EventListener
{
    [DispId(0)]
    public void handler(IHTMLEventObj evt) 
    {
        MessageBox.Show("message received");
    }
}

void AddEventListener()
{
    var evtListener = new EventListener();
    var window = ((IHTMLDocument2)browser.Document).parentWindow as IHTMLWindow3;
    window.attachEvent("MyCustomEvent", evtListener); //or try onMyCustomEvent
}
user3275863
  • 65
  • 1
  • 6
-1

It looks like there are a few different ways you can approach the problem.

Here are a couple of links that look relevant:

Hope this helps...

-- Lance

Community
  • 1
  • 1
Lance Leonard
  • 3,285
  • 3
  • 16
  • 15