• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Browser extensions
  5. Your first extension

Your first extension

In This Article
  1. Writing the extension
    1. manifest.json
    2. icons/border-48.png
    3. borderify.js
  2. Trying it out
    1. Installing
    2. Testing
  3. Packaging and publishing
  4. What's next?

In this article we'll walk through creating an extension for Firefox, from start to finish. The extension 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 extension

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 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 extension.
  • description is optional, but recommended: it's displayed in the Add-ons Manager.
  • icons is optional, but recommended: it allows you to specify an icon for the extension, that will be shown in the Add-ons Manager.

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.

  • Learn more about content scripts.
  • Learn more about match patterns.

In some situations you need to specify an ID for your extension. If you do need to specify an add-on ID, include the  applications key in manifest.json and set its id property:

"applications": {
  "gecko": {
    "id": "borderify@example.com"
  }
}

icons/border-48.png

The extension should have an icon. This will be shown next to the extension'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.

  • Learn more about specifying icons.

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.

  • Learn more about content scripts.

Trying it out

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

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

Installing

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

The extension will now be installed, and will stay until you restart Firefox.

Alternatively, you can run the extension from the command line using the web-ext tool.

Testing

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

Don't try it on addons.mozilla.org, though! Content scripts are currently blocked on that domain.

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 extensions's files by clicking the "Reload" button in about:debugging. You can see the changes right away:

  • Learn more about loading extensions

Packaging and publishing

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

What's next?

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

  • reading more about the anatomy of an extensions
  • writing a more complex extension
  • reading about the JavaScript APIs available for extensions.

Document Tags and Contributors

Tags: 
  • Guide
  • WebExtensions
 Contributors to this page: andrewtruongmoz, wbamberg, arai, openupyourmind, Makyen, Sebastianz, kumar303, nitwit1984, cosmik, Noitidart, hashedhyphen
 Last updated by: andrewtruongmoz, Jul 6, 2017, 2:50:34 PM
See also
  1. Browser extensions
  2. Getting started
    1. What are extensions?
    2. Your first extension
    3. Your second extension
    4. Anatomy of an extension
    5. Example extensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. User interface
    1. Introduction
    2. Toolbar button
    3. Address bar button
    4. Sidebar
    5. Context menu items
    6. Options page
    7. Bundled web pages
    8. Notifications
    9. Address bar suggestions
    10. Developer tools panels
  5. Concepts
    1. Using the JavaScript APIs
    2. Content scripts
    3. Match patterns
    4. Internationalization
    5. Content Security Policy
    6. Native messaging
  6. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox extension
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
    7. Differences between desktop and Android
  7. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Developing for Firefox for Android
    4. Getting started with web-ext
    5. web-ext command reference
    6. Extensions and the Add-on ID
    7. Publishing your extension
  8. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browserSettings
    6. browsingData
    7. commands
    8. contextMenus
    9. contextualIdentities
    10. cookies
    11. devtools.inspectedWindow
    12. devtools.network
    13. devtools.panels
    14. downloads
    15. events
    16. extension
    17. extensionTypes
    18. history
    19. i18n
    20. identity
    21. idle
    22. management
    23. notifications
    24. omnibox
    25. pageAction
    26. permissions
    27. privacy
    28. proxy
    29. runtime
    30. sessions
    31. sidebarAction
    32. storage
    33. tabs
    34. theme
    35. topSites
    36. types
    37. webNavigation
    38. webRequest
    39. windows
  9. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_settings_overrides
    6. chrome_url_overrides
    7. commands
    8. content_scripts
    9. content_security_policy
    10. default_locale
    11. description
    12. developer
    13. devtools_page
    14. homepage_url
    15. icons
    16. incognito
    17. manifest_version
    18. name
    19. omnibox
    20. optional_permissions
    21. options_ui
    22. page_action
    23. permissions
    24. protocol_handlers
    25. short_name
    26. sidebar_action
    27. theme
    28. version
    29. web_accessible_resources
  10. Themes
  11. Publishing add-ons
  12. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Creating an appealing listing
    4. Review policies
    5. Developer agreement
    6. Featured add-ons
    7. Contact addons.mozilla.org
  13. Community and support
  14. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel
  15. Legacy add-ons
  16. Legacy technologies
    1. Add-on SDK
    2. Legacy Firefox for Android
    3. Bootstrapped extensions
    4. Overlay extensions