Firefox Extensions

From CSE330 Wiki
Jump to navigationJump to search


XUL (XML User-interface Language) is a markup language that describes the user interface. It is at the heart of Mozilla based browsers. Through XUL, you can defined input controls, toolbars, menus, dialogs, keyboard shortcuts and many more. XUL could be used for Mozilla/Firefox extensions or stand-alone applications. In fact, Firefox's interface is written in XUL. If the security permissions are enabled, you can also use XUL to remotely modify the interface of the browser.

You can access a XUL file just using regular URL. An alternative is to package several related files to a chrome package. There are two ways to create chrome packages, a JAR file and a chrome directory. The jar file is basically a ZIP file with chrome directory structure. Alternatively, you can directly put the chrome directory inside your browser's profile directory. Either way, chrome packages usually have three type of data: content (main windows, scripts etc.), skin (theme related files such as images, css etc.),, locale (local language related files).

If you have installed a chrome packages, you can access its XUL files directly using Firefox specific protocol (chrome://). For example, since Firefox user-interface is written in XUL, you can access its xul files using chrome:// protocol. The typical format is

chrome://<package name>/<part>/<file.xul> 

For example,


will show you the main Firefox user-interface.

Hello World in XUL

The following code displays Hello World message on the browser:

<?xml version="1.0"?>
<?xml-stylesheet href="chrome://global/skin/" type="text/css"?>

    title="Hello  World"
    <label value="Hello World!!! "/>


In this code, the first two lines specify that this is an XML file and it uses a stylesheet. Then XML file specifies a window element. This will be the main container. Its attributes contain an id and title as well as its orientation. Also there is a specifial attribute


This is a special attribute that informs the browser all its children are XUL.

This windows only child is a label with the value Hello World!!!.

Once you have the above XUL file, you can load it in your firefox and observe its behavior.

XUL Packages

The new file are added to they system using chrome packages. Packages are basically list of XUL files and supplementary material. Once a package is installed, the files can be accessed using chrome:// protocol. Firefox becomes aware of installed packages through manifest files. The purpose of these files is to map the protocol access to the actual locations in the physcical disk. While Mozilla reads all manifest files under chrome directory, Firefox reads only extentions/*/chrome.manifest names. Manifest files have the keywords content, skin and locale for each type.

content package-name file-path

skin package-name skin-name file-path
locale package-name skin-name file-path

file-path could be a physicall directory file:///path-name-here, or a JAR file, jar:<filename.jar>!/<path_in_archive>.

Installing XUL Packages

Both XUL installation and extension installation uses the same technique. You first need to create an XPI file. This is basically a zip file with some specific files inside. Lets see an example installation of hello.xul (above)

In order to install hello.xul, we will install a dummy extension. First, create a directory (lets call it helloworld) and edit a new manifest file


 content helloworld chrome/

This tells Firefox that the package helloworld has its contents under chrome directory. Next create this directory under helloworld and copy your hello.xul there. Finally, create install.rdf file under helloworld directory.

install.rdf is a specifal file that describes your extension. In the very basic format, you need at least the following:


<?xml version="1.0"?>
<RDF xmlns=""

        <Description about="urn:mozilla:install-manifest">

                <em:name>Hello World </em:name>
                <em:description>This is my hellow world</em:description>

                <!-- Firefox -->



<em:id> describes the unique id for your extension. There are several ID generators available. You can use a web based one, such as [1]. <em:name>, <em:version>, <em:description>,

<em:description>,<em:creator> are straight forward.

<em:targetApplication> describes which applications can use this extension, all tools have their unique ids.

At this step, your should have the following directory structure.

|-- chrome
|   `-- hello.xul
|-- chrome.manifest
`-- install.rdf

Inside helloworld directory, create a new zip file:

zip -r ../helloworld.xpi *

and open this file in Firefox. This will install your new extenstion. Now, you can reach your hello.xul with


Modifying Firefox

chrome command overlay overwrites XUL pages with other XULs. The format is

overlay chrome://first-package  chrome://overwrite-the-first-package

You can use this command to overwrite Firefox XUL files. For example,

content news_blogger chrome/content/
overlay chrome://browser/content/browser.xul chrome://news_blogger/content/news_blogger.xul

first defines a new chrome for news_blogger and then let its definitions replace existing firefox definitions. This way you can define new sidebars, toolboxes or context menus.


For toolboxes, you can use <toolbox element. After toolbox, you can define a toolbar in which you can put other items.

<?xml version="1.0"?>
<overlay id="picture-overlay"   xmlns="">
<toolbox id="navigator-toolbox">
<toolbar id="picture-toolbar" accesskey="T" class="chromeclass-toolbar" context="toolbar-context-menu" toolbarname="picture toolbar" hidden="false" persist="hidden">
<label id="picture-url" value="this is a test"/>


You can define a sidebar using a second XUL file. The first XUL defines a menu item. In this case, it is attached to the View→Sidebar menu using the viewSidebarMenuid, which opens or closes News Blogger in the sidebar when activated. The menu item is then hooked up to a broadcaster via the observes attribute. The new broadcaster for the sidebar is overlaid into Firefox's main broadcaster set. The menu-item attributes are abstracted to the overlay simply to allow it to be used in more than one place.

<?xml version="1.0"?>

<overlay id="news_bloggerOverlay"

  <menupopup id="viewSidebarMenu">
    <menuitem key="key_NewsSidebar" observes="viewNewsSidebar"  />

  <broadcasterset id="mainBroadcasterSet">
    <broadcaster id="viewNewsSidebar"
                 sidebartitle="News Blogger"
                 oncommand="toggleSidebar('viewNewsSidebar');" />


Then in the second XUL, you can define the sidebar the way you want.

<?xml version="1.0"?>

<page id="sbEmptySidebar" title="News Blogger;"

...... your XUL elements are here......


Context Menu Changes

You can add new context menu (right mouse button) items using overlay

<?xml version="1.0"?>

<overlay id="news_bloggerOverlay"
   <popup id="contentAreaContextMenu" insertafter="context-searchselect">
   <menuseparator id="menuseperator" />
      <menuitem id="add_article" label="Add Article" oncommand="AddArticle(event); event.preventBubble()"/>
      <menuitem id="add_picture" label="Add Picture " oncommand="AddPicture(event); event.preventBubble()"/>


This adds two new menu items just after a firefox defined item (context-searchselect). It also sets the function to be called when these items are selected.

Adding Javascript

You can add javascript after within window,overlay or page elements. You can also refer to an existing file.

<script type="application/x-javascript" src="chrome://news_blogger/content/news_blogger.js"/>

<?xml version="1.0"?>

<overlay id="news_bloggerOverlay"
<script type="application/x-javascript" src="chrome://news_blogger/content/news_blogger.js"/>


Adding HTML Elements

If you want to use regular HTML elements, you need to include XHTML definition in your root tag.

<window id="topWindow"  xmlns=""
       xmlns:html="" >

Once you have included XHTML definitions, then you can put regular HTML elements using <html:TAG> and </html:TAG>. For example, you can define an input field.

<html:input type="text" name="myinput"/> 


Example set of XUL elements

XUL Element References