How to trap bookmark menu events in Firefox
This came up today on the mozdev project-owners list. My response was aimed at the one person asking for help, but I think the problem is general enough (and I banged my head enough while solving it), that it seemed worth mentioning here.
The question was, how can an extension catch the event when a user clicks on a bookmark entry? In recent IE View test builds, I needed to react the same way to links accessed via bookmarks as to those clicked within a page. Trapping normal links - easy. Bookmark clicks - not. Here’s what I did.
When my overlay initializes, I stash a reference to the original BookmarksCommand.openOneBookmark function, then assign BookmarksCommand.openOneBookmark to my own function. When a link is clicked, I get the event. If it’s a link that meets my launch-in-IE list, I take action; otherwise, I call the old handler, passing in all the parameters I received. The relevant code is:
if (BookmarksCommand && BookmarksCommand.openOneBookmark)
{
// openOneBookmark: function (aURI, aTargetBrowser, aDS)
gOldOpenOneBookmark = BookmarksCommand.openOneBookmark;
BookmarksCommand.openOneBookmark = function(aURI, aTargetBrowser, aDS) {
var url = BookmarksUtils.getProperty(aURI, NC_NS+"URL", aDS);
if (IeView.forceIe(url))
{
IeView.ieViewLaunch("Internet Explorer.lnk", url);
return;
}
else
{
gOldOpenOneBookmark(aURI, aTargetBrowser, aDS);
}
};
}
See the complete overlay source for context.
This works, but apart from just making me feel dirty, it could easily break if the BookmarksCommand object changes. Anyone have a better way?