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

编写GCLI命令

Types

Out of the box we support:

  • The obvious basic types: string, number, boolean
  • 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 date and file types, but they're not ready yet. For now you should use string.

进阶文档

GCLI项目包含了几页的相关文档:

访问Firefox

当一个命令被执行时,会传递两个参数, argscontext, 在GCLI文档中有详细的解释. Firefox提供的environment对象包含了两个属性:

  • chromeDocument: 允许访问浏览器界面.
  • contentDocument: 允许访问当前正在浏览的网页.

例如:

gcli.addCommand({
  name: 'closebrowserwindow',
  exec: function(args, context) {
    context.environment.chromeDocument.defaultView.close();
  }
});

或:

gcli.addCommand({
  name: 'countdivs',
  exec: function(args, context) {
    return context.environment.contentDocument.querySelectorAll('div').length;
  }
});

国际化/本地化

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.addCommand({
  name: 'greet',
  description: gcli.lookup("greetDesc")
  ...
});

单元测试

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

第一个例子:

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

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

第二个例子:

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/ ],
});

更多信息

一些可能有用的链接 :

文档标签和贡献者

 此页面的贡献者: ziyunfei
 最后编辑者: ziyunfei,