Code Samples

This page contains sample code that you can use with the Custom Toolbar Button tutorial.

The samples here are designed to work in Firefox, Thunderbird, Sunbird and SeaMonkey, except where the text says otherwise.

Launch a file on your computer

To launch a file on your computer, use code like this. Change the first line to specify the file that you want to launch. You can launch an executable file or some other file that your operating system knows how to open.

Note: On operating systems that use backslash characters, double each backslash character. On operating systems that use forward-slash characters, specify the file in the normal way.

const path = "C:\\WINDOWS\\CHARMAP.EXE"

var file = Components
  .classes["@mozilla.org/file/local;1"]
  .createInstance(Components.interfaces.nsILocalFile)

file.initWithPath(path)
file.launch()

Open a web page

If your button is in Firefox or SeaMonkey, use code like this to open a web page. Change the first line to specify the page that you want to open:

const url = "http://www.mozilla.org/"

document
  .getElementById("content")
  .webNavigation
  .loadURI(url, 0, null, null, null)

If your button is in Thunderbird or Sunbird, use code like this to open a web page. Change the first line to specify the page that you want to open:

const url = "http://www.mozilla.org/"

var uri = Components
  .classes["@mozilla.org/network/simple-uri;1"]
  .getService(Components.interfaces.nsIURI)

uri.spec = url

Components
  .classes["@mozilla.org/uriloader/external-protocol-service;1"]
  .getService(Components.interfaces.nsIExternalProtocolService)
  .loadUrl(uri)

Compose an e-mail

To compose an e-mail, use the same code as in the section Open a web page above. Change the first line to specify:

const url = "mailto://"

Open an application window

To open one of the application's windows, use code like this. If the window already exists, this code just focusses it. Otherwise it opens the window:

const name = "...internal name of the window..."
const uri = "...chrome URI of the window..."

var w = Components
  .classes["@mozilla.org/appshell/window-mediator;1"]
  .getService(Components.interfaces.nsIWindowMediator)
  .getMostRecentWindow(name)

if (w) w.focus()
else Components
  .classes["@mozilla.org/embedcomp/window-watcher;1"]
  .getService(Components.interfaces.nsIWindowWatcher)
  .openWindow(null, uri, name, "chrome,resizable", null)

Change the first two lines to specify the window that you want to open. Here are the names and URIs of some windows:

Thunderbird
Configuration
const name = "Preferences:ConfigManager"
const uri = "chrome://global/content/config.xul"
Passwords
const name = "Toolkit:PasswordManager"
const uri = chrome://messenger/content/preferenc...wpasswords.xul"
Mail &
Newsgroups
const name = "mail:3pane"
const uri = "chrome://messenger/content/"
Sunbird
Passwords*
const name = "Toolkit:PasswordManager"
const uri = "chrome://passwordmgr/content/passwordManager.xul"
Seamonkey
Navigator
const name = "navigator:browser"
const uri = "chrome://navigator/content/"
Mail &
Newsgroups
const name = "mail:3pane"
const uri = "chrome://messenger/content/"
Composer
const name = "composer:html"
const uri = "chrome://editor/content/"
Address Book
const name = "mail:addressbook"
const uri = "chrome://messenger/content/addressbo...ddressbook.xul"
IRC Chat
const name = "irc:chatzilla"
const uri = "chrome://chatzilla/content/"
Calendar
const name = "calendarMainWindow"
const uri = "chrome://calendar/content/"

* At the time of writing, Sunbird's Passwords window is broken

Close the current window

To close the window containing the button, possibly leaving other windows open:

close()

Exit the application

To exit the application, first closing all its windows:

Components
  .classes['@mozilla.org/toolkit/app-startup;1']
  .getService(Components.interfaces.nsIAppStartup)
  .quit(Components.interfaces.nsIAppStartup.eAttemptQuit)