views:

23

answers:

1

I would like to write a firefox extension. This extension is not a generic extension but work specifically for a domain where I need to highlight specific html components.

How should I do that? I just want the js loaded when the user is browsing a specific domain.

My current overaly.js is basically empty (generated by the Extension Wizard):

var myextension = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("myextension-strings");
  },

  onMenuItemCommand: function(e) {
    var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
                                  .getService(Components.interfaces.nsIPromptService);
    promptService.alert(window, this.strings.getString("helloMessageTitle"),
                                this.strings.getString("helloMessage"));
  },

  onToolbarButtonCommand: function(e) {
    // just reuse the function above.  you can change this, obviously!
    myextension.onMenuItemCommand(e);
  }
};

window.addEventListener("load", myextension.onLoad, false);

And my ff-overlay.xul is:

myextension.onFirefoxLoad = function(event) {
  document.getElementById("contentAreaContextMenu")
          .addEventListener("popupshowing", function (e){ myextension.showFirefoxContextMenu(e); }, false);
};

myextension.showFirefoxContextMenu = function(event) {
  // show or hide the menuitem based on what the context menu is on
  document.getElementById("context-myextension").hidden = gContextMenu.onImage;
};

window.addEventListener("load", myextension.onFirefoxLoad, false);

I was thinking to go neanderthal and do a check inside myextension.onFirefoxLoad to see if the currentpage is the one I want but that requires the user to click the proper item on the context menu.

+2  A: 

I'm not totally following what you have because both of those look like JS files, not XUL files. But what you probably want to do is listen for the load event coming from the web pages that are loaded. Then, in your event loader, just look at each page that loads and see whether it's coming from the specific domain you want.

A great (though not always quite as easy as it sounds) way to find out how to do something in a Firefox addon is to find another addon that does something similar. DOM Inspector and Inspect Context are your friends! The first such addon that comes to mind in this case is WikiTrust so you could try looking at that one to see if it gives you any inspiration.

MatrixFrog
It was `onPageLoad` I was looking for. I have a second question related to this. Since I don't need to edit the browser UI, do I still need `overlay.xul`? At this point should it be a default `overlay.xul` with only a `script` tag to call the extension js file?
dierre
Yeah, I guess so. Just include your script tag, and maybe a stringbundle to externalize your strings. Then if you later decide to add something else, you can always do so.
MatrixFrog