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.

Revision 1084881 of Your first WebExtension

  • Revision slug: Mozilla/Add-ons/WebExtensions/Your_first_WebExtension
  • Revision title: Your first WebExtension
  • Revision id: 1084881
  • Created:
  • Creator: cosmik
  • Is current revision? No
  • Comment I tried following this article in Firefox 47 on Mac OS, however installing of the add-on failed with an error message in browser console complaining about the missing "applications" key in the manifest.

Revision Content

{{AddonSidebar}}

In this article we'll walk through creating a WebExtension for Firefox, from start to finish. The add-on just adds a red border to any pages loaded from "mozilla.org" or any of its subdomains.

The source code for this example is on GitHub: https://github.com/mdn/webextensions-examples/tree/master/borderify.

First, you'll need Firefox version 45 or later.

Writing the WebExtension

Create a new directory and navigate to it:

mkdir borderify
cd borderify

manifest.json

Now create a new file called "manifest.json" directly under the "borderify" directory. Give it the following contents:

{

  "manifest_version": 2,
  "name": "Borderify",
  "version": "1.0",

  "description": "Adds a solid red border to all webpages matching mozilla.org.",

  "icons": {
    "48": "icons/border-48.png"
  },

  "content_scripts": [
    {
      "matches": ["*://*.mozilla.org/*"],
      "js": ["borderify.js"]
    }
  ]

}
  • The first three keys: manifest_version, name, and version, are mandatory and contain basic metadata for the add-on.
  • description is optional, but recommended: it's displayed in the Add-on Manager.
  • icons is optional, but recommended: it allows you to specify an icon for the add-on, that will be shown in the Add-ons Manager.

For certain Firefox versions, an additional key called applications may need to be added in manifest.json:

"applications": {
  "gecko": {
    "id": "[email protected]",
    "strict_min_version": "42.0",
    "strict_max_version": "50.*",
    "update_url": "https://example.com/updates.json"
  }
}

The most interesting key here is content_scripts, which tells Firefox to load a script into Web pages whose URL matches a specific pattern. In this case, we're asking Firefox to load a script called "borderify.js" into all HTTP or HTTPS pages served from "mozilla.org" or any of its subdomains.

icons/border-48.png

The add-on should have an icon. This will be shown next to the add-on's listing in the Add-ons Manager. Our manifest.json promised that we would have an icon at "icons/border-48.png".

Create the "icons" directory directly under the "borderify" directory. Save an icon there named "border-48.png".  You could use the one from our example, which is taken from the Google Material Design iconset, and is used under the terms of the Creative Commons Attribution-ShareAlike license.

If you choose to supply your own icon, It should be 48x48 pixels. You could also supply a 96x96 pixel icon, for high-resolution displays, and if you do this it will be specified as the 96 property of the icons object in manifest.json:

"icons": {
  "48": "icons/border-48.png",
  "96": "icons/border-96.png"
}

Alternatively, you could supply an SVG file here, and it will be scaled correctly.

borderify.js

Finally, create a file called "borderify.js" directly under the "borderify" directory. Give it this content:

document.body.style.border = "5px solid red";

This script will be loaded into the pages that match the pattern given in the content_scripts manifest.json key. The script has direct access to the document, just like scripts loaded by the page itself.

Testing it out

First, double check that you have the right files in the right places:

borderify/
    icons/
        border-48.png
    borderify.js
    manifest.json

Open "about:debugging" in Firefox, click "Load Temporary Add-on" and select any file in your add-on's directory:

{{EmbedYouTube("cer9EUKegG4")}}

The add-on will now be installed, and will stay until you restart Firefox.

Now try visiting a page under "mozilla.org", and you should see the red border round the page:

{{EmbedYouTube("rxBQl2Z9IBQ")}}

Try experimenting a bit. Edit the content script to change the color of the border, or do something else to the page content. Save the content script, then reload the add-on's files by clicking the "Reload" button in about:debugging. You can see the changes right away:

{{EmbedYouTube("NuajE60jfGY")}}

Packaging and publishing

For other people to use your add-on, you need to package it and submit it to Mozilla for signing. To learn more about that, see "Publishing your WebExtension".

What's next?

Now you've got an idea of the process of developing a WebExtension for Firefox, try:

Revision Source

<div>{{AddonSidebar}}</div>

<p>In this article we'll walk through creating a WebExtension for Firefox, from start to finish. The add-on just adds a red border to any pages loaded from "mozilla.org" or any of its subdomains.</p>

<p>The source code for this example is on GitHub: <a href="https://github.com/mdn/webextensions-examples/tree/master/borderify">https://github.com/mdn/webextensions-examples/tree/master/borderify</a>.</p>

<p>First, you'll need Firefox version 45 or later.</p>

<h2 id="Writing_the_WebExtension">Writing the WebExtension</h2>

<p>Create a new directory and navigate to it:</p>

<pre class="brush: bash">
mkdir borderify
cd borderify</pre>

<h3 id="manifest.json">manifest.json</h3>

<p>Now create a new file called "manifest.json" directly under the "borderify" directory. Give it the following contents:</p>

<pre class="brush: json">
{

  "manifest_version": 2,
  "name": "Borderify",
  "version": "1.0",

  "description": "Adds a solid red border to all webpages matching mozilla.org.",

  "icons": {
    "48": "icons/border-48.png"
  },

  "content_scripts": [
    {
      "matches": ["*://*.mozilla.org/*"],
      "js": ["borderify.js"]
    }
  ]

}</pre>

<ul>
 <li>The first three keys: <code><a href="/en-US/Add-ons/WebExtensions/manifest.json/manifest_version">manifest_version</a></code>, <code><a href="/en-US/Add-ons/WebExtensions/manifest.json/name">name</a></code>, and <code><a href="/en-US/Add-ons/WebExtensions/manifest.json/version">version</a></code>, are mandatory and contain basic metadata for the add-on.</li>
 <li><code><a href="/en-US/Add-ons/WebExtensions/manifest.json/description">description</a></code> is optional, but recommended: it's displayed in the Add-on Manager.</li>
 <li><code><a href="/en-US/Add-ons/WebExtensions/manifest.json/icons">icons</a></code> is optional, but recommended: it allows you to specify an icon for the add-on, that will be shown in the Add-ons Manager.</li>
</ul>

<p>For certain Firefox versions, an additional key called <code><a href="https://developer.mozilla.org/en-US/Add-ons/WebExtensions/manifest.json/applications">applications</a></code> may need to be added in manifest.json:</p>

<pre class="brush: json line-numbers  language-json">
<code class="language-json"><span class="key token">"applications":</span> <span class="punctuation token">{</span>
  <span class="key token">"gecko":</span> <span class="punctuation token">{</span>
    <span class="key token">"id":</span> <span class="string token">"[email protected]"</span><span class="punctuation token">,</span>
    <span class="key token">"strict_min_version":</span> <span class="string token">"42.0"</span><span class="punctuation token">,</span>
    <span class="key token">"strict_max_version":</span> <span class="string token">"50.*"</span><span class="punctuation token">,</span>
    <span class="key token">"update_url":</span> <span class="string token">"https://example.com/updates.json"</span>
  <span class="punctuation token">}</span>
<span class="punctuation token">}</span></code></pre>

<p>The most interesting key here is <code><a href="/en-US/Add-ons/WebExtensions/manifest.json/content_scripts">content_scripts</a></code>, which tells Firefox to load a script into Web pages whose URL matches a specific pattern. In this case, we're asking Firefox to load a script called "borderify.js" into all HTTP or HTTPS pages served from "mozilla.org" or any of its subdomains.</p>

<ul>
 <li><a href="/en-US/Add-ons/WebExtensions/Content_scripts">Learn more about content scripts.</a></li>
 <li><a href="/en-US/Add-ons/WebExtensions/Match_patterns">Learn more about match patterns</a>.</li>
</ul>

<h3 id="iconsborder-48.png">icons/border-48.png</h3>

<p>The add-on should have an icon. This will be shown next to the add-on's listing in the Add-ons Manager. Our manifest.json promised that we would have an icon at "icons/border-48.png".</p>

<p>Create the "icons" directory directly under the "borderify" directory. Save an icon there named "border-48.png".&nbsp; You could use <a href="https://github.com/mdn/webextensions-examples/blob/master/borderify/icons/border-48.png">the one from our example</a>, which is taken from the Google Material Design iconset, and is used under the terms of the <a href="https://creativecommons.org/licenses/by-sa/3.0/">Creative Commons Attribution-ShareAlike</a> license.</p>

<p>If you choose to supply your own icon, It should be 48x48 pixels. You could also supply a 96x96 pixel icon, for high-resolution displays, and if you do this it will be specified as the <code>96</code> property of the <code>icons</code> object in manifest.json:</p>

<pre class="brush: json line-numbers  language-json">
<code class="language-json"><span class="key token">"icons":</span> <span class="punctuation token">{</span>
  <span class="key token">"48":</span> <span class="string token">"icons/border-48.png",
  "96": "icons/border-96.png"</span>
<span class="punctuation token">}</span></code></pre>

<p>Alternatively, you could supply an SVG file here, and it will be scaled correctly.</p>

<ul>
 <li><a href="/en-US/Add-ons/WebExtensions/manifest.json/icons">Learn more about specifying icons.</a></li>
</ul>

<h3 id="borderify.js">borderify.js</h3>

<p>Finally, create a file called "borderify.js" directly under the "borderify" directory. Give it this content:</p>

<pre class="brush: js">
document.body.style.border = "5px solid red";</pre>

<p>This script will be loaded into the pages that match the pattern given in the <code>content_scripts</code> manifest.json key. The script has direct access to the document, just like scripts loaded by the page itself.</p>

<ul>
 <li><a href="/en-US/Add-ons/WebExtensions/Content_scripts">Learn more about content scripts.</a></li>
</ul>

<h2 id="Testing_it_out">Testing it out</h2>

<p>First, double check that you have the right files in the right places:</p>

<pre>
borderify/
    icons/
        border-48.png
    borderify.js
    manifest.json</pre>

<p>Open "about:debugging" in Firefox, click "Load Temporary Add-on" and select any file in your add-on's directory:</p>

<p>{{EmbedYouTube("cer9EUKegG4")}}</p>

<p>The add-on will now be installed, and will stay until you restart Firefox.</p>

<p>Now try visiting a page under "mozilla.org", and you should see the red border round the page:</p>

<p>{{EmbedYouTube("rxBQl2Z9IBQ")}}</p>

<p>Try experimenting a bit. Edit the content script to change the color of the border, or do something else to the page content. Save the content script, then reload the add-on's files by clicking the "Reload" button in about:debugging. You can see the changes right away:</p>

<p>{{EmbedYouTube("NuajE60jfGY")}}</p>

<ul>
 <li><a href="/en-US/Add-ons/WebExtensions/Temporary_Installation_in_Firefox">Learn more about loading add-ons</a></li>
</ul>

<h2 id="Packaging_and_publishing">Packaging and publishing</h2>

<p>For other people to use your add-on, you need to package it and submit it to Mozilla for signing. To learn more about that, see <a href="/en-US/docs/Mozilla/Add-ons/WebExtensions/Publishing_your_WebExtension">"Publishing your WebExtension"</a>.</p>

<h2 id="What's_next">What's next?</h2>

<p>Now you've got an idea of the process of developing a WebExtension for Firefox, try:</p>

<ul>
 <li><a href="/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension">reading more about the anatomy of WebExtensions</a></li>
 <li><a href="/en-US/Add-ons/WebExtensions/Your_second_WebExtension">writing a more complex WebExtension</a></li>
 <li><a href="/en-US/Add-ons/WebExtensions/API">reading about the JavaScript APIs available to WebExtensions.</a></li>
</ul>
Revert to this revision