MDN’s new design is in Beta! A sneak peek:

Displaying annotations

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

Deprecated in Firefox 29 and removed in Firefox 38.

Warning: this tutorial relies on the since-removed Widget API and no longer works with Firefox.

The widget API is deprecated from Firefox 29 onwards. Please see the ui module for replacements. In particular, for a simple button, try the action button or toggle button APIs, and for a more complex widget try the toolbar or sidebar APIs.

In this chapter we'll use a page-mod to locate elements of web pages that have annotations associated with them, and a panel to display the annotations.

Matcher page-mod

Matcher Content Script

The content script for the matcher page-mod is initialized with a list of all the annotations that the user has created.

When a page is loaded the matcher searches the DOM for elements that match annotations. If it finds any it binds functions to that element's mouseenter and mouseleave events to send messages to the main module, asking it to show or hide the annotation.

Like the selector, the matcher also listens for the window's unload event and on unload sends a detach message to the main module, so the add-on can clean it up.

The complete content script is here:

self.on('message', function onMessage(annotations) {
    function(annotation) {
      if(annotation.url == document.location.toString()) {

  $('.annotated').css('border', 'solid 3px yellow');

  $('.annotated').bind('mouseenter', function(event) {
    self.port.emit('show', $(this).attr('annotation'));

  $('.annotated').bind('mouseleave', function() {

function createAnchor(annotation) {
  annotationAnchorAncestor = $('#' + annotation.ancestorId);
  annotationAnchor = $(annotationAnchorAncestor).parent().find(
                     ':contains(' + annotation.anchorText + ')').last();
  $(annotationAnchor).attr('annotation', annotation.annotationText);

Save this in data as matcher.js.

Updating main.js

First, initialize an array to hold workers associated with the matcher's content scripts:

var matchers = [];

In the main function, add the code to create the matcher:

var matcher = pageMod.PageMod({
  include: ['*'],
  contentScriptWhen: 'ready',
  contentScriptFile: [data.url('jquery-1.4.2.min.js'),
  onAttach: function(worker) {
    if( {
    worker.port.on('show', function(data) {
      annotation.content = data;;
    worker.port.on('hide', function() {
      annotation.content = null;
    worker.on('detach', function () {
      detachWorker(this, matchers);

When a new page is loaded the function assigned to onAttach is called. This function:

  • initializes the content script instance with the current set of annotations
  • provides a handler for messages from that content script, handling the three messages - show, hide and detach - that the content script might send
  • adds the worker to an array, so we it can send messages back later.

Then in the module's scope implement a function to update the matcher's workers, and edit handleNewAnnotation to call this new function when the user enters a new annotation:

function updateMatchers() {
  matchers.forEach(function (matcher) {
function handleNewAnnotation(annotationText, anchor) {
  var newAnnotation = new Annotation(annotationText, anchor);;

Annotation panel

The annotation panel just shows the content of an annotation.

There are two files associated with the annotation panel:

  • a simple HTML file to use as a template
  • a simple content script to build the panel's content

These files will live in a new subdirectory of data which we'll call annotation.

Annotation panel HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="" xml:lang="en">
    <style media="all" type="text/css">
body {
        font: 100% arial, helvetica, sans-serif;
        background-color: #F5F5F5;
div {
<div id="annotation">

Save this in data/annotation as annotation.html.

Annotation panel Content Script

The annotation panel has a minimal content script that sets the text:

self.on('message', function(message) {

Save this in data/annotation as annotation.js.

Updating main.js

Finally, update main.js with the code to construct the annotation panel:

var annotation = panels.Panel({
  width: 200,
  height: 180,
  contentURL: data.url('annotation/annotation.html'),
  contentScriptFile: [data.url('jquery-1.4.2.min.js'),
  onShow: function() {

Execute cfx run one last time. Activate the annotator and enter an annotation. You should see a yellow border around the item you annotated:

When you move your mouse over the item, the annotation should appear:

Obviously this add-on isn't complete yet. It could do with more beautiful styling, it certainly needs a way to delete annotations, it should deal with OverQuota more reliably, and the matcher could be made to match more reliably.

But we hope this gives you an idea of the things that are possible with the modules in the SDK.

Document Tags and Contributors

 Contributors to this page: wbamberg, Canuckistani, asmacdo
 Last updated by: wbamberg,