Setting up extension development environment: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(How to use an unpacked version of Thunderbird/Firefox; Script how to pack an extension)
Line 1: Line 1:
{{extdev}}
{{extdev}}
This describes some changes you can do to your system to make [[extension development]] easier. See also [[Getting started with extension development]].
This describes some changes you can do to your system to make [[extension development]] easier. See also [[Getting started with extension development]].
== Using an unpacked version of Thunderbird/Firefox for development ==
It's often necessary to extract all the jar files of the Thunderbird/Firefox/Mozilla Suite version you want to develop with:
It makes debugging easier, you can include the code directly in the original sources, and you can make links from the sources to your extension sources. That describes what you need to do, at the example of the Thunderbird headerScroll extension (should apply to all) (Windows users: The tools I use aren't on your system, sorry):
# Extract the <code>.tar.gz</code> distribution file to a directory, e.g. "thunderbird-1.0.6"
# <code>cd "thunderbird-1.0.6/chrome"</code>
# <code>for file in *.jar; do unzip $file; done</code>
# <code>perl -pi.orig -e 's/(jar:)|(\/[^.\/]+\.jar!)//g' chrome.rdf installed-chrome.txt</code>
# <code>ln -s /path/to/extension/src messenger-extensionname</code> - see below for an explanation
# open <code>mail/content/messenger/mailWindowOverlay.xul</code> and add <code><?xul-overlay href="chrome://messenger-headerscroll/content/headerscroll_overlay.xul"?></code> at the beginning after the other overlay defs - DON'T do <code>content/messenger-headerscroll/headerscroll_overlay.xul</code> - it's a chrome directory name, so we have to omit the <code>content</code> part
# add <code>content,install,url,resource:/chrome/messenger-headerscroll/content/messenger-headerscroll/</code> to <code>chrome/installed-chrome.txt</code> to give permissions
==== The explanation to point 5: ====
I don't like to mix the Thunderbird sources and my extension sources, so the extension code lies in a different folder. The structure of that is as follows:
headerScroll/
+- install.rdf
+- dist/
    +- all released versions go here
+- src/
    +- content/
      + messenger-headerscroll/
        +- contents.rdf
        +- headerscroll_overlay.js
        +- headerscroll_overlay.xul
        +- other files
So all I do with the <code>ln -s</code> is to link the extension's directory <code>src</code> directory in the <code>thunderbird/chrome/</code> directory, so that it's accessible from there.


== Set development prefs ==
== Set development prefs ==
Line 25: Line 52:
== Development profile ==
== Development profile ==
Having the above prefs set is very helpful when developing extensions, but they will slow down Firefox a bit, particularly at startup and when opening new browser windows, especially if you have many extensions installed. For these reasons you may choose to use a separate profile for development.
Having the above prefs set is very helpful when developing extensions, but they will slow down Firefox a bit, particularly at startup and when opening new browser windows, especially if you have many extensions installed. For these reasons you may choose to use a separate profile for development.
ATTENTIONS: Especially in Thunderbird you should use a separate profile as you could destroy your old one. Development versions and Nightlies often harm a profile!


You can run two instances of Firefox using separate profiles if you set '''MOZ_NO_REMOTE''' environment variable to '''1'''. For example, on Windows you can use the following bat file to run Firefox with development profile, whether "normal" Firefox is already running or not. (Assuming your development profile is called ''"dev"''):
You can run two instances of Firefox using separate profiles if you set '''MOZ_NO_REMOTE''' environment variable to '''1'''. For example, on Windows you can use the following bat file to run Firefox with development profile, whether "normal" Firefox is already running or not. (Assuming your development profile is called ''"dev"''):
Line 31: Line 60:


To run Firefox with default profile just run "firefox" or "firefox -p default", as usual.
To run Firefox with default profile just run "firefox" or "firefox -p default", as usual.
== Packing your extension ==
The extension has to be packed for distribution as .xpi file. To automate this, you can use a small shell script which does the zipping for you:
#!/bin/bash
buildid=$1
<nowiki>[[ "$buildid" == "" ]]</nowiki> && buildid=cvs-`date +%Y%m%d`
xpifile=dispmua-$buildid.xpi
cd src
zip -r ../messenger-dispmua.jar . -x "CVS"
cd ..
mkdir chrome
mv messenger-dispmua.jar chrome/
rm "$xpifile"
zip "$xpifile" chrome/messenger-dispmua.jar install.rdf
rm -r chrome/
echo -n Created file:
ls "$xpifile"
Of course you have to change the jar file name from messenger-dispmua to one matching your extension, but you see how to do it.
One more thing: If you pass a parameter to the script, it is taken as the version and appended to the name of the xpi file

Revision as of 08:00, 27 August 2005

This page is part of the extension development documentation project.

Ask your questions in MozillaZine Forums. Also try browsing example code.

Note: development documentation is in process of being moved to Mozilla Development Center (MDC).

This describes some changes you can do to your system to make extension development easier. See also Getting started with extension development.

Using an unpacked version of Thunderbird/Firefox for development

It's often necessary to extract all the jar files of the Thunderbird/Firefox/Mozilla Suite version you want to develop with: It makes debugging easier, you can include the code directly in the original sources, and you can make links from the sources to your extension sources. That describes what you need to do, at the example of the Thunderbird headerScroll extension (should apply to all) (Windows users: The tools I use aren't on your system, sorry):

  1. Extract the .tar.gz distribution file to a directory, e.g. "thunderbird-1.0.6"
  2. cd "thunderbird-1.0.6/chrome"
  3. for file in *.jar; do unzip $file; done
  4. perl -pi.orig -e 's/(jar:)|(\/[^.\/]+\.jar!)//g' chrome.rdf installed-chrome.txt
  5. ln -s /path/to/extension/src messenger-extensionname - see below for an explanation
  6. open mail/content/messenger/mailWindowOverlay.xul and add <?xul-overlay href="chrome://messenger-headerscroll/content/headerscroll_overlay.xul"?> at the beginning after the other overlay defs - DON'T do content/messenger-headerscroll/headerscroll_overlay.xul - it's a chrome directory name, so we have to omit the content part
  7. add content,install,url,resource:/chrome/messenger-headerscroll/content/messenger-headerscroll/ to chrome/installed-chrome.txt to give permissions

The explanation to point 5:

I don't like to mix the Thunderbird sources and my extension sources, so the extension code lies in a different folder. The structure of that is as follows:

headerScroll/
+- install.rdf
+- dist/
   +- all released versions go here
+- src/
   +- content/
      + messenger-headerscroll/
        +- contents.rdf
        +- headerscroll_overlay.js
        +- headerscroll_overlay.xul
        +- other files

So all I do with the ln -s is to link the extension's directory src directory in the thunderbird/chrome/ directory, so that it's accessible from there.

Set development prefs

Before you start doing any development, you should set some preferences to make life easier:

  • javascript.options.showInConsole = true. Logs errors in chrome files to the JavaScript Console to make debugging easier.
  • nglayout.debug.disable_xul_cache = true. Disables the XUL cache so that changes do not require a restart (more).
  • browser.dom.window.dump.enabled = true. Enables the use of the dump() statement to print to the standard console. (The application must be started using the -console flag; read more)
  • javascript.options.strict = true. Enables strict Javascript warnings in the JavaScript Console. Note that since many people have this setting turned off when developing, you will see lots of warnings for problems with their code in addition to warnings for your own extension.

Install development extensions

It is also a good idea to install a few developer extensions:

  • The DOM Inspector is useful when you are trying to find the id of XUL elements which you wish to modify.
  • Not really a developer extension, but the JavaScript Console can be surprisingly useful. Open it every time you have made changes to your JS files; it serves as a handy syntax checker (but make sure you have set the preferences listed above).
  • For more advanced Javascript aid, check out Venkman, the Mozilla-based Javascript debugger (project home, latest info).
  • Extension developer's extension for Firefox/Thunderbird/Mozilla (discussion). Includes these tools:
    • JS Shell—execute statements from main application window. Features tab completion!
    • JS Environment—run code snippets
    • XUL Editor—XUL editor with real-time preview
    • HTML Editor—same for HTML
    • Extension Builder—a tool for editing install.rdf, packaging and installing your extension; still under development
    • As a bonus, it can toggle the debugging prefs listed above with a single menu click.
  • Console Filter helps you to find the relevant errors in the JavaScript Console.

Development profile

Having the above prefs set is very helpful when developing extensions, but they will slow down Firefox a bit, particularly at startup and when opening new browser windows, especially if you have many extensions installed. For these reasons you may choose to use a separate profile for development.

ATTENTIONS: Especially in Thunderbird you should use a separate profile as you could destroy your old one. Development versions and Nightlies often harm a profile!

You can run two instances of Firefox using separate profiles if you set MOZ_NO_REMOTE environment variable to 1. For example, on Windows you can use the following bat file to run Firefox with development profile, whether "normal" Firefox is already running or not. (Assuming your development profile is called "dev"):

set MOZ_NO_REMOTE=1
firefox -p dev

To run Firefox with default profile just run "firefox" or "firefox -p default", as usual.


Packing your extension

The extension has to be packed for distribution as .xpi file. To automate this, you can use a small shell script which does the zipping for you:

#!/bin/bash
buildid=$1
[[ "$buildid" == "" ]] && buildid=cvs-`date +%Y%m%d`
xpifile=dispmua-$buildid.xpi
cd src
zip -r ../messenger-dispmua.jar . -x "CVS"
cd ..
mkdir chrome
mv messenger-dispmua.jar chrome/
rm "$xpifile"
zip "$xpifile" chrome/messenger-dispmua.jar install.rdf
rm -r chrome/
echo -n Created file:
ls "$xpifile"

Of course you have to change the jar file name from messenger-dispmua to one matching your extension, but you see how to do it. One more thing: If you pass a parameter to the script, it is taken as the version and appended to the name of the xpi file