I'm working on my first Firefox extension - an attempt to get Firefox to support some version of XPointer that will work on HTML documents.

If you haven't read about XPointer before, you can start at my recent blog post here.

At the moment, things are pretty much in a raw, not-at-all-ready-for-prime-time state, though I've traveled down some lanes that I've always wanted to try out (specifically I've got my first real taste of XUL and XBL). Unfortunately it took me a while to get even a little clue on some of this stuff and it can be a bit discouraging. Here's hoping the Mozilla documentation improves both in coverage and recency - but I'm not holding my breath.

The goal is: I want to modify the view of an HTML document if any XPointer parts show up in the URL bar.

I started with simply adding a "load" event handler to the gBrowser. This worked fine any time a page had to load (including in a new tab or in the existing tab). However, what I noticed was that a "load" event would not fire if the link was in the same page (i.e. just the "hash" part of the URL had changed) if you clicked a link or when you typed it in. So if I'm at page foo.html and that page has a link to foo.html#element(bar), when I click that link I do not get a "load" event.

So that led me into learning a bit more about XBL to try and add some bindings to the chrome.

Where I sit now: I've got XBL defining an evalXPointer() method on the "urlbar" XUL widget. When a new document is loaded, I invoke this method. Also, when a new URL is entered into the location bar (and the user presses Enter), I invoke this method. Finally, if the "Go" button is clicked I also invoke this method. All my "logic" sits in JavaScript in the XBL at the moment.

Anyway, I kind of get the idea that what I'm doing is not appropriate. That's what community feedback is all about. The above bindings seem kind of like a hack. What I really want to do is get notified when any link has been activated (by click or otherwise) and regardless of whether this link causes a "load" event or if it's in the same page. Does anybody know the sane way to do this?

Better yet, is there a way to detect when a browser tab's address has changed in a definitive way? I don't want to look at every keypress.

§401 · October 29, 2007 · Firefox, Questions, Software, Technology, Web, XML · · [Print]

2 Comments to “Firefox Extensions and Detecting A Clicked Link”

  1. Dean says:

    Hi,

    I’m curious if there is any chance to detect Firefox version (either 3 or 2) and apply different CSS accordingly.

    Thanks.

  2. @Dean: I’m not aware of any way of doing this declaratively – but you could always use JS and sniff the browser string – not the most reliable means, but it might do.