mozilla

Revision 236494 of Scratchpad

  • Revision slug: Tools/GCLI/Scratchpad
  • Revision title: Scratchpad
  • Revision id: 236494
  • Created:
  • Creator: joewalker
  • Is current revision? No
  • Comment 41 words added, 40 words removed

Revision Content

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, 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:///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) { }

Revision Source

<p>Creating commands using the Scratchpad requires chrome privileges. See the <a href="/en/Tools/Scratchpad#Using_Scratchpad_to_access_Firefox_internals" title="en/Tools/Scratchpad#Using_Scratchpad_to_access_Firefox_internals">scratchpad documentation for details on how to enable this feature</a>.</p>
<h2>Basic Command Template</h2>
<p>The simplest command looks like this:</p>
<div class="highlight"> <div class="line" id="LC1"> <pre class="line"><span class="nx">Components</span><span class="p">.</span><span class="nx">utils</span><span class="p">.</span><span class="kr">import</span><span class="p">(</span><span class="s2">"resource:///modules/devtools/gcli.jsm"</span><span class="p">);</span>

<span class="nx">gcli</span><span class="p">.</span><span class="nx">addCommand</span><span class="p">({</span>
  <span class="nx">name</span><span class="o">:</span> <span class="s1">'hello'</span><span class="p">,</span>
  <span class="nx">description</span><span class="o">:</span> <span class="s1">'Show a message'</span><span class="p">,</span>
  <span class="nx">params</span><span class="o">:</span> <span class="p">[</span>
    <span class="p">{</span>
      <span class="nx">name</span><span class="o">:</span> <span class="s1">'name'</span><span class="p">,</span>
      <span class="nx">type</span><span class="o">:</span> <span class="s1">'string'</span><span class="p">,</span>
      <span class="nx">description</span><span class="o">:</span> <span class="s1">'Who to say hello to'</span><span class="p">,</span>
    <span class="p">}</span>
  <span class="p">],</span>
  <span class="nx">exec</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="s1">'Good morning, '</span> <span class="o">+</span> <span class="nx">args</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">});</span>
</pre> </div> <div class="line">Paste that into a new chrome scratchpad, and execute it (CTRL+R) to add a 'hello' command to the command line.</div> <div class="line"> <h2>More Information</h2> <ul> <li><a href="/en/Tools/GCLI/Writing_GCLI_Commands" title="en/Tools/GCLI/Writing_GCLI_Commands">Documentation about writing commands</a></li> </ul> </div> <h2 class="line">Add-on Compatibility</h2> <div class="highlight"> <p>It's possible to make commands so they can be trivially converted into add-ons. Just use the following template:</p> <pre class="line"><span class="nx">Components</span><span class="p">.</span><span class="nx">utils</span><span class="p">.</span><span class="kr">import</span><span class="p">(</span><span class="s2">"resource:///modules/devtools/gcli.jsm"</span><span class="p">);</span>

<span class="kd">var</span> <span class="nx">helloCommandSpec</span> <span class="o">=</span> <span class="p">{</span>
  <span class="nx">name</span><span class="o">:</span> <span class="s1">'hello'</span><span class="p">,</span>
  <span class="nx">description</span><span class="o">:</span> <span class="s1">'Show a message'</span><span class="p">,</span>
  <span class="nx">params</span><span class="o">:</span> <span class="p">[</span>
    <span class="p">{</span>
      <span class="nx">name</span><span class="o">:</span> <span class="s1">'name'</span><span class="p">,</span>
      <span class="nx">type</span><span class="o">:</span> <span class="s1">'string'</span><span class="p">,</span>
      <span class="nx">description</span><span class="o">:</span> <span class="s1">'Who to say hello to'</span><span class="p">,</span>
    <span class="p">}</span>
  <span class="p">],</span>
  <span class="nx">exec</span><span class="o">:</span> <span class="kd">function</span><span class="p">(</span><span class="nx">args</span><span class="p">,</span> <span class="nx">context</span><span class="p">)</span> <span class="p">{</span>
    <span class="k">return</span> <span class="s1">'Good morning, '</span> <span class="o">+</span> <span class="nx">args</span><span class="p">.</span><span class="nx">name</span><span class="p">;</span>
  <span class="p">}</span>
<span class="p">}</span>

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

<span class="kd">function</span> <span class="nx">startup</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">reason</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">gcli</span><span class="p">.</span><span class="nx">addCommand</span><span class="p">(</span><span class="nx">helloCommandSpec</span><span class="p">);</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">shutdown</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">reason</span><span class="p">)</span> <span class="p">{</span>
  // addCommand automatically removes any old commands, but to be tidy ...
  <span class="nx">gcli</span><span class="p">.</span><span class="nx">removeCommand</span><span class="p">(</span><span class="nx">helloCommandSpec</span><span class="p">);</span>
<span class="p">}</span>

<span class="kd">var</span> <span class="nx">__SCRATCHPAD__</span> <span class="o">=</span> <span class="o">!</span><span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nb">window</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"undefined"</span><span class="p">);</span>
<span class="k">if</span> <span class="p">(</span><span class="nx">__SCRATCHPAD__</span> <span class="o">&amp;&amp;</span> <span class="p">(</span><span class="k">typeof</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">gBrowser</span><span class="p">)</span> <span class="o">==</span> <span class="s2">"undefined"</span><span class="p">))</span> <span class="p">{</span>
  <span class="k">throw</span> <span class="k">new</span> <span class="nb">Error</span><span class="p">(</span><span class="s2">"Must be run in a browser scratchpad."</span><span class="p">);</span>
<span class="p">}</span>

<span class="k">if</span> <span class="p">(</span><span class="nx">__SCRATCHPAD__</span><span class="p">)</span> <span class="p">{</span>
  <span class="nx">shutdown</span><span class="p">();</span>
  <span class="nx">startup</span><span class="p">();</span>
<span class="p">}</span>

<span class="kd">function</span> <span class="nx">install</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">reason</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
<span class="kd">function</span> <span class="nx">uninstall</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="nx">reason</span><span class="p">)</span> <span class="p">{</span> <span class="p">}</span>
</pre> </div>
</div>
Revert to this revision