How a Firefox Extension Works

Basic anatomy:

  1. When Firefox starts up, it looks in your profile folder for an extensions folder, which contains a folder called
  2. In there it finds a file called install.rdf, which contains an id element that matches the name of the folder (, and some other metadata about the extension.
  3. There's also a file called chrome.manifest, which maps the files in your extension to chrome: urls and lists which overlays and stylesheets to load.
  4. This chrome.manifest says that the XUL file chrome/content/overlay.xul (accessed at chrome://yourextension/content/overlay.xul) overlays the browser's chrome://browser/content/browser.xul.
  5. The overlay file is loaded and mapped onto the source XUL file: if the id attribute of any element in the overlay matches the id attribute of an element in the destination XUL, everything within the source element is inserted into the destination element.
  6. This chrome.manifest also says that the CSS file chrome/skin/overlay.css (accessed at chrome://yourextension/skin/overlay.css) should be attached to the browser's chrome://global/content/customizeToolbar.xul.
  7. The overlay contains a script element which loads a Javascript file, chrome/content/overlay.js, which sets up the functions for your extension.
  8. There's an oncommand attribute on the toolbar button, so when it's clicked your extension's run function will run.

Here are some examples of the files described above:

install.rdf (no idea why this is RDF rather than single-namespaced XML):

<?xml version="1.0"?>
<rdf:RDF xmlns:rdf="" xmlns="">
  <rdf:Description about="urn:mozilla:install-manifest">
    <name>Your Extension</name>
    <description>Description of your extension</description>
    <!-- Firefox --> 


# chrome path     # chrome root             # relative path
content           yourextension             chrome/content/
skin              yourextension classic     chrome/skin/
# type  # destination                                 # source
overlay chrome://browser/content/browser.xul          chrome://yourextension/content/overlay.xul
style   chrome://global/content/customizeToolbar.xul  chrome://yourextension/skin/overlay.css


<overlay xmlns="" id="yourextension-overlay">
  <script type="application/x-javascript" src="chrome://yourextension/content/overlay.js"/>
  <toolbarpalette id="BrowserToolbarPalette">
      <toolbarbutton id="yourextension-toolbar-button" class="toolbarbutton-1" oncommand=""/>


@namespace url("");
#yourextension-toolbar-button {
	list-style-image: url(chrome://yourextension/skin/buttons.png);
	-moz-image-region: rect(0 33px 24px 0);
#yourextension-toolbar-button:hover {
	-moz-image-region: rect(0 66px 24px 33px);
toolbar[iconsize="small"] #yourextension-toolbar-button {
	-moz-image-region: rect(0 88px 16px 66px);
toolbar[iconsize="small"] #yourextension-toolbar-button:hover {
	-moz-image-region: rect(0 110px 16px 88px);


var YourExtension = {
  run: function(){
    // do something

See this posting extension for a working example. It's just a zip file, so you can open it and look inside.

Here's a bash script that might be useful for compiling the extension into an XPI file for distribution:

APP_NAME=yourextension # edit this
rm "$APP_NAME.xpi"
# generate the XPI by zipping everything in the current directory, excluding .DS_Store files
zip -r "$APP_NAME.xpi" `find . -path -prune -o -type f -print | grep -v '' | grep -v 'DS_Store'`