views:

338

answers:

2

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?

A: 

You're going to find there is no 100% solution to do this for IE during page load. It's not your fault: IE's event model is a giant spaghetti mass of asynchronous code which makes it very difficult to get right. If you've got a solution that is 95%, I say be happy and move on.

jeffamaphone
If you look at HtmlPageEventManager, you'll see I've come pretty far along with a flexible solution that accommondates the giant spaghetti mass. The idea is to tuck away the details into this reusable component so that others can benefit from it. You're right that I really do need to move on, though. That's why I'm hoping to find someone who will help me improve it. The problem now is that everytime the user refreshes the webpage, events stop being handled and my plug-in stops functioning.
HappyNomad
Yeah, trying to handle Refresh event in IE is a total buzz kill. You can have a look at what John Lister did in his DemoButton thing. http://code.google.com/p/pricegoblin/wiki/DemoButton
jeffamaphone
+1  A: 

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.

HappyNomad