Scratchpad

This article is in need of a technical review.

Creating commands using the Scratchpad requires chrome privileges. See the scratchpad documentation for details on how to enable this feature.

Basic Command Template

The simplest command looks like this:

Components.utils.import("resource:///modules/devtools/gcli.jsm");

gcli.addCommand({
  name: 'hello',
  description: 'Show a message',
  params: [
    {
      name: 'name',
      type: 'string',
      description: 'Who to say hello to',
    }
  ],
  exec: function(args, context) {
    return 'Good morning, ' + args.name;
  }
});
Paste that into a new chrome scratchpad, set the environment to "browser" (in the scratchpad's menu) and execute it (CTRL+R) to add a 'hello' command to the command line.

Add-on Compatibility

It's possible to make commands so they can be trivially converted into add-ons. Just use the following template:

Components.utils.import("resource://gre/modules/devtools/gcli.jsm");

var helloCommandSpec = {
  name: 'hello',
  description: 'Show a message',
  params: [
    {
      name: 'name',
      type: 'string',
      description: 'Who to say hello to',
    }
  ],
  exec: function(args, context) {
    return 'Good morning, ' + args.name;
  }
}

// The boiler plate below allows this module to be exported as a Firefox add-on
// If you don't need add-on compatibility, you just need to call
//   gcli.addCommand(helloCommandSpec);

function startup(data, reason) {
  gcli.addCommand(helloCommandSpec);
}

function shutdown(data, reason) {
  // addCommand automatically removes any old commands, but to be tidy ...
  gcli.removeCommand(helloCommandSpec);
}

var __SCRATCHPAD__ = !(typeof(window) == "undefined");
if (__SCRATCHPAD__ && (typeof(window.gBrowser) == "undefined")) {
  throw new Error("Must be run in a browser scratchpad.");
}

if (__SCRATCHPAD__) {
  shutdown();
  startup();
}

function install(data, reason) { }
function uninstall(data, reason) { }

 

Document Tags and Contributors

Last updated by: yfdyh000,