Scratchpad

使用Scratchpad创建命令需要在chrome上下文执行特权代码. 查看scratchpad文档了解如何激活该特性.

基本命令模板

最简单的命令可以使用下面的代码添加:

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;
  }
});
将上面的代码粘贴到一个scratchpad窗口中,将"环境"菜单设置为"浏览器" ,然后执行代码 (CTRL+R),这样就添加了一个 'hello'命令到命令行中.

附加组件兼容性

可以使用下面的代码模板制作一个用来添加自定义命令的附加组件:

Components.utils.import("resource:///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) { }

文档标签和贡献者

最后编辑者: ziyunfei,