Visit Mozilla.org

Extension Frequently Asked Questions

From MDC

(Redirected from Extension FAQ)

This is quick set of answers to the most common issues with extension development. They are currently written with mostly Firefox in mind, but most if not all should easily translate to SeaMonkey, Thunderbird or any of the other applications.

If you're looking for a place to get started, try our tutorial, Building an Extension or MozillaZine's Getting started tutorial. Use the Extension Wizard to generate a template to start with.

Be sure to set the development preferences.

Contents

[edit] Debugging

You should set development preferences before attempting to debug your extension.

The Venkman JavaScript debugger may be useful in complex cases, remember to turn off the "Debug -> Exclude Browser Files" option when working on extension code.

[edit] How can I see errors in my code?

After you set the preference javascript.options.showInConsole to true, the errors will be reported to the Error Console. By logging all JavaScript errors into the console, it becomes much easier to track down the bugs in your code.

[edit] How can I display what my extension is doing?

You can use alert(), dump(), Components.utils.reportError(), or the console service to display variable data and debugging text. You can also try to use the Venkman JavaScript Debugger add-on.

[edit] Why doesn't my script run properly?

If your script doesn't function as expected, the first thing you should do is check the Error Console (see above).

One common mistake is trying to access the DOM of a window before it is fully loaded. This happens if you place the initialization code at the top level of your script (i.e. outside of any functions). The fix is to use a load event listener to delay your code until the window has finished loading:

function exampleBrowserStartup(event)
{
  // place your startup code here
}
window.addEventListener("load", exampleBrowserStartup, false);

[edit] Accessing the document of a webpage doesn't work

To get the document of the current webpage from a browser.xul overlay, you should use content.document, instead of just document which is the document of the browser's window itself. See Working with windows in chrome code for details.

Also, by default XPCNativeWrapper prevents you from accessing script-defined objects in the webpage and doing some other things.

[edit] I get an XML parsing error, but the file looks fine!

A common source of parsing errors (some red text with a -------------^ underneath) is an & or a < character in your script or an attribute value, which has special meaning in XML. For example:

<button oncommand="window.open('http://example.com/q?param1=value&param2=val2')"/>

or

<script>function lesser(a,b) { return a < b ? a : b; }</script>

The problem can be solved in one of the following ways:

  1. replace the character with its non XML conflicting representation (Ex: "&" -> "&amp;", "<" -> "&lt;")
  2. (in case it's a text node, such as script) place CDATA tags around it:
    <script><![CDATA[
       function lesser(a,b) {
         return a < b ? a : b;
       }
     ]]></script>
  3. Put your script in a separate file and include it using:
    <script type="application/x-javascript" src="our.js"/>

[edit] Example code

The easiest way to find out what code you need to use in order to do something useful is to find an extension (or part of Mozilla itself) that does it and look at its code. (The XPI and JAR files use the ZIP format.)

There is also some documentation. See the list of extension-related articles on MDC, Code snippets, and Example code page on MozillaZine.

[edit] Where can I get more help?

Please see Extensions:Other Resources and Extensions:Community.

Before asking for help, be sure to set the debugging prefs and check the Error Console for related messages. Also, don't forget to do at least a simple search before asking. And read this FAQ!