Reddit Example

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 addons.mozilla.org (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.

This example uses the action button API, which is only available from Firefox 29 onwards.

This example add-on creates a panel containing the mobile version of Reddit. When the user clicks on the title of a story in the panel, the add-on opens the linked story in a new tab in the main browser window.

To accomplish this the add-on needs to run a content script in the context of the Reddit page which intercepts mouse clicks on each title link and fetches the link's target URL. The content script then needs to send the URL to the add-on script.

This is the complete add-on script:

var data = require("sdk/self").data;

var button = require("sdk/ui/button/action").ActionButton({
  id: "reddit-panel",
  label: "Reddit Panel",
  icon: "./icon-16.png",
  onClick: function() {
    reddit_panel.show();
  }
});

var reddit_panel = require("sdk/panel").Panel({
  width: 240,
  height: 320,
  contentURL: "http://www.reddit.com/.mobile?keep_extension=True",
  contentScriptFile: [data.url("jquery-2.1.0.min.js"),
                      data.url("panel.js")]
});

reddit_panel.port.on("click", function(url) {
  require("sdk/tabs").open(url);
});

This code supplies two content scripts to the panel's constructor in the contentScriptFile option: the jQuery library and the script that intercepts link clicks.

Finally, it registers a listener to the user-defined click event which in turn passes the URL into the open function of the tabs module.

This is the panel.js content script that intercepts link clicks:

$(window).click(function (event) {
  var t = event.target;

  // Don't intercept the click if it isn't on a link.
  if (t.nodeName != "A")
    return;

  // Don't intercept the click if it was on one of the links in the header
  // or next/previous footer, since those links should load in the panel itself.
  if ($(t).parents('#header').length || $(t).parents('.nextprev').length)
    return;

  // Intercept the click, passing it to the addon, which will load it in a tab.
  event.stopPropagation();
  event.preventDefault();
  self.port.emit('click', t.toString());
});

This script uses jQuery to interact with the DOM of the page and the self.port.emit function to pass URLs back to the add-on script.

To run this example you'll also have to have an icon file named "icon-16.png" saved in your add-on's "data" directory. You could download this icon: .

Document Tags and Contributors

 Contributors to this page: wbamberg, dkocho4, evold
 Last updated by: wbamberg,