Reddit Example

We are planning to deprecate the use by Firefox add-ons of the techniques described in this document.

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 instead.

Add-ons developed using these techniques might not work with multiprocess Firefox (e10s), which is already the default in Firefox Nightly and Firefox Developer Edition, and will soon be the default in Beta and Release versions of Firefox. We have documentation on making your add-ons multiprocess-compatible, but it will be more future-proof for you to migrate to WebExtensions.

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,