Search code examples
javascriptfirefoxfirefox-addondom-eventsbookmarks

How to detect clicks on history or bookmark entries?


Within my Firefox extension I'd like to detect the source of a page load -- for example, after clicking on: a link, a history entry or a bookmark. So far I've manage to detect the click on a link and using the Back/Forward history buttons.

However, I fail to detect clicks on items of the history menu, let alone clicks on items in the window of the history library (popping up when clicking "Show All History"). Same problem for the bookmark menu and the bookmark library window.

Is there a more or less straightforward way to accomplish this? For the bookmark menu I could think of adding a click listener to all bookmarks.


Solution

  • All clicks on history and bookmark items (both in main browser window and separate window/sidebar) will call PlacesUIUtils.openNodeWithEvent() or PlacesUIUtils.openNodeIn() (the latter is used when the user explicitly chooses where to open the item from the context menu). You can extend these functions using an approach like this:

    Components.utils.import("resource:///modules/PlacesUIUtils.jsm");
    if (!("myExtensionHooked" in PlacesUIUtils.openNodeWithEvent))
    {
      var origOpenNodeWithEvent = PlacesUIUtils.openNodeWithEvent;
      PlacesUIUtils.openNodeWithEvent = function(node)
      {
        // Remember node.uri here - user chose this URL
        ...
    
        // Call original function
        return origOpenNodeWithEvent.apply(this, arguments);
      };
      PlacesUIUtils.openNodeWithEvent.myExtensionHooked = true;
    }
    

    And similarly for the other function. Note that you should do this only once per browsing session - PlacesUIUtils object is shared by all browser windows. That's the purpose of the myExtensionHooked property in the example code (you should change it to something that is unique to your extension).