Search code examples
.netinternet-explorerdominteropbho

Listening to HTML Events from BHO


I've been working on a BHO/toolbar written in C# that listens to HTML events raised on the browser's current webpage. The solution includes a reusable class called HtmlPageEventManager whose purpose is to subscribe to a given list of HTML events for each new webpage that's opened. A goal is to attach handlers as soon as the user can begin interacting with page elements, even before the page load is complete. Using this class is simple -- just call the constructor:

var evts = new List() {
    HtmlEvent.onclick, HtmlEvent.ondblclick, HtmlEvent.onkeydown,
    HtmlEvent.onselectstart, HtmlEvent.onselectionchange,
    HtmlEvent.onfocus, HtmlEvent.onselect
};
new HtmlPageEventManager( this, evts, this.HtmlEventHandler );

Please download my solution here to try it out and send me feedback. Feel free to use it in your own projects if you find it useful. Although it works well, there are occasions when it fails to attach the events. I've had difficulty pinpointing those circumstances. So I could use some help improving upon HtmlPageEventManager.

My solution references SpicIE's assemblies but they're not included in the above download, so you will need to get it from the SpicIE website . Btw, this is probably a good time to ask: how popular is SpicIE? Any better tool to use?


Solution

  • Yeah! I overcame the "Refresh" issue by subscribing to the BHO's DownloadComplete event, where I again call my "RegisterEventHandlers" helper method that attaches the HTML event handlers. The code jeffamaphone linked to was helpful. I incorporated its idea of a "normalPageLoad" member variable to conditionally call RegisterEventHandlers. I also looked at the accompanying forum post. I can't understand the emphasis on the parent window's Loaded event, though. I got my code working without even doing that part. Anyway, I think HtmlPageEventManager is fully functional now -- knock on wood. Please give me a shout if you find a case in which it's still not doing its job.