Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Writing GCLI Commands

The documentation that comes with GCLI has a section on writing commands. The documentation that follows describes the GCLI support provided in Firefox.

Types

Out of the box Firefox supports:

  • The obvious GCLI basic types: string, number, boolean, date,
  • setting, settingValue: i.e. prefs (settingValue is string, number, or boolean depending on the underlying type of the setting)
  • node: A single node on the page referenced by CSS expression
  • resource: A CSS or JavaScript file in the page

We're also working on file types, but they're currently incomplete. For now you should use string.

Further Documentation

The GCLI project contains several pages of relevant documentation:

Access to Firefox

When a command is executed, it is passed 2 parameters, args and context. These are both explained in the GCLI documentation. Firefox provides an environment which contains the following:

  • chromeDocument, chromeWindow: Allows access to the browser environment
  • document,window: Allows access to the current web page
  • target: Access to the target object for the current web page

So for example:

gcli.addItems([{
  name: 'closebrowserwindow',
  runAt: 'client',
  exec: function(args, context) {
    context.environment.chromeWindow.close();
  }
}]);

Or:

gcli.addItems([{
  name: 'countdivs',
  exec: function(args, context) {
    return context.environment.document.querySelectorAll('div').length;
  }
}]);

Note: In order to access the chrome environment (e.g. context.environment.chromeWindow), you need to set runAt: 'client' (see for example: paintflashing.js from the built-in commands).

Internationalization / Localization

The way GCLI does localization (for the web) doesn't work with commands that are shipped with Firefox.

To add a command that will only ever be used embedded in Firefox, this is the way to go. Your strings should be stored in browser/locales/en-US/chrome/browser/devtools/gclicommands.properties,
And you should access them using gcli.lookup(...) or gcli.lookupFormat().

For examples of existing commands, take a look in browser/devtools/webconsole/GcliCommands.jsm, which contains most of the
current GCLI commands. If you will be adding a number of new commands, then consider starting a new JSM.

Your command will then look something like this:

gcli.addItems([{
  name: 'greet',
  description: gcli.lookup("greetDesc")
  ...
}]);

Unit Tests

The command line comes with a framework to make it easy to write tests.

Your test() method will look something like this:

function test() {
  DeveloperToolbarTest.test(TEST_URI, function(browser, tab) {
    testThis(browser, tab);
    testThat(browser, tab);
    finish();
  });
}

There are 2 functions to help you testing - checkInputStatus() which checks to see that the command line can understand input properly and know what's allowed and disallowed, and exec() which checks that commands have the correct operation when executed.

checkInputStatus

All calls to checkInputStatus() need an input string in the typed property, and optionally a cursor property to specify where the cursor is when the checks are made. It will be common to leave this out, in which case the cursor is assumed to be at the end of the command line.

DeveloperToolbarTest.checkInputStatus({
  typed:  "echo hi",
  // checks go here
});

There are 3 states that are important to the command line:

  • VALID: Obvious - this is ok and ready to go
  • INCOMPLETE: This is not valid, but it's possible to make it valid just continuing to add characters
  • ERROR: This is wrong, and no amount of adding characters will help

The distinction between INCOMPLETE and ERROR is obvious when you consider the command 'cat README.TX' - assuming that the file to be displayed is README.TXT, as it stands it's not right, but we shoudn't be marking it as an error either.

These states apply to individual characters (and decide the underline state) and to the command line as a whole, which is generally the worst of these statuses, plus other checks.

There are 5 checks that can be made by checkInputStatus():

  • status: One of the strings "VALID", "ERROR", "INCOMPLETE"
  • emptyParameters: And array containing the parameters that still need typing
  • directTabText: What will be added to the command line when TAB is pressed if the completion is a simple extension of what is there already
  • arrowTabText: As above for when the completion text isn't an extension of what's there - e.g. for fuzzy matching
  • markup: One char for char on the input being the first letter of the status of that char. e.g. "VVVIIIEEE"

For example:

DeveloperToolbarTest.checkInputStatus({
  typed:  "edit c",
  markup: "VVVVVI",
  status: "ERROR",
  directTabText: "ss#style2",
  emptyParameters: [ " [line]" ],
});

exec

The exec() test is similar to checkInputStatus() except that it's more about checking the output and effect of running the command. The typed property is the same, however the checks are different:

  • args: an object that matches the args object passed to exec
  • outputMatch: A RegExp or Array of Regexps which should all match the textual content of the output
  • blankOutput: true if the command should produce no output
  • completed: false if the command should execute asynchronously

First example:

DeveloperToolbarTest.exec({
  typed: "console close",
  args: {},
  blankOutput: true,
});

ok(!(hud.hudId in imported.HUDService.hudReferences), "console closed");

Second Example:

DeveloperToolbarTest.exec({
  typed: "pref set devtools.editor.tabsize 9",
  args: {
    setting: imports.settings.getSetting("devtools.editor.tabsize"),
    value: 9
  },
  completed: true,
  outputMatch: [ /void your warranty/, /I promise/ ],
});

More Info

Some additional links which could be useful:

Document Tags and Contributors

 Contributors to this page: aplaice, jwalker, joewalker
 Last updated by: aplaice,