Feed content access API

Firefox 2 and Thunderbird 2 introduce a series of interfaces that make it easy for extension authors to access RSS and Atom feeds.

Feed interfaces

nsIFeed
Represents an RSS or Atom feed.
nsIFeedContainer
A base class subclassed by several of the feed-related interfaces.
nsIFeedElementBase
A base class subclassed by several of the other feed-related interfaces.
nsIFeedEntry
Represents a single entry in an RSS or Atom feed.
nsIFeedGenerator
Describes the software that generated an RSS or atom feed.
nsIFeedPerson
Represents a person. Contains the person's name, email address, and their home page address.
nsIFeedProcessor
Parses RSS and Atom feeds.
nsIFeedProgressListener
Implemented by the program that wants to parse an RSS or Atom feed to receive messages during the parsing process.
nsIFeedResult
Describes the result of parsing a feed.
nsIFeedResultListener
Implemented by the program that wants to parse an RSS or Atom feed to receive notification when parsing is complete.
nsIFeedTextConstruct
Represents text values in a feed; includes functions that let you fetch the text as plain text or HTML.
nsIScriptableUnescapeHTML
A utility class that unescapes HTML strings.

Example: Reading a feed from the Web

It's actually quite easy to read and parse a feed. Use an XMLHttpRequest to load the feed, then pass its string to an nsIFeedProcessor to parse the feed.

Loading the feed and sending it to the parser is done using code similar to this:

  fetch: function(feedUrl)
  {
    var httpRequest = null;
    function infoReceived() {
      var data = httpRequest.responseText;
      var ioService = Components.classes['@mozilla.org/network/io-service;1']
                                         .getService(Components.interfaces.nsIIOService);
      var uri = ioService.newURI(feedUrl, null, null);
      if (data.length) {
        var parser = Components.classes["@mozilla.org/feed-processor;1"]
                                        .createInstance(Components.interfaces.nsIFeedProcessor);
        var listener = new FeedTestResultListener();
        try {
          parser.listener = listener;
          parser.parseFromString(data, uri);
        }
        catch(e) {
          alert("Error parsing feed.");
        }
      }
    }
    httpRequest = new XMLHttpRequest();
    httpRequest.open("GET", feedUrl, true);
    try {
      httpRequest.onload = infoReceived;
      httpRequest.send(null);
    }
    catch(e) {
      alert(e);
    }
  }

The nsIFeedProcessor interface lets you parse the feed data from several possible sources; in this case, we're loading a document into a string, then parsing that string using its parseFromString() method. However, you could also parse it from a file using parseFromStream(), or directly from an URL using parseAsync().

The actual processing of the parsed feed is done by a method called handleResult() on the FeedTestResultListener object. That code looks like this:

    FeedTestResultListener.prototype = {
      handleResult: function(result) {
        var feed = result.doc;
        feed.QueryInterface(Components.interfaces.nsIFeed);
        // Open a new window
        var win = window.open("", "FeedTest_Window");
        var doc = win.document.wrappedJSObject;
        doc.open();
        // Write the HTML header and page title
        doc.write("<html><head><title>Feed: " + feed.title.text + "</title></head><body>");
        doc.write("<h1>" + feed.title.text + "</h1><p>");
        var itemArray = feed.items;
        var numItems = itemArray.length;
        // Write the article information
        if (!numItems) {
          doc.write("<i>No news is good news!</i>");
        }
        else {
          var i;
          var theEntry;
          var theUrl;
          var info;
          for (i=0; i<numItems; i++) {
            theEntry = itemArray.queryElementAt(i, Components.interfaces.nsIFeedEntry);
            if (theEntry) {
              theUrl = 
              doc.write('<b><a href="' + theEntry.link.resolve("") + '">' + theEntry.title.text + '</a></b><br>');
              if (theEntry.summary) {
                info = theEntry.summary.text + "<p><hr><p>";
              }
              else {
                info = theEntry.content.text + "<p><hr><p>";
              }
              doc.write("<blockquote>" + info);
              doc.write("</blockquote><p>");
            }
          }
        }
        // Close the document; we're done!
        doc.write("</body></html>");
        doc.close();
      }
    }

The handleResult() function receives as its argument an nsIFeedResult that describes a feed; its doc property is an nsIFeed that contains all the feed data.

To get the title of the feed, you look at the feed.title property. The title is an nsIFeedTextConstruct that can represent the text in various formats; we get its text property to fetch the feed's title as HTML-encoded text. We could, alternatively, use its plainText() method to get a copy of the title translated into plain text.

We then scan the array of items in the feed by looking at the feed.items nsIArray. This array contains nsIFeedEntry objects that describe each feed item.

We build the contents of the document by looking at the title, link, summary, and content properties for each item. The full URL of the link is retrieved using the link's resolve() method.

To initialize a fetch of the feed, we simply call fetch(url). This opens a new window containing the feed contents, with each item's title a clickable link that takes you to the article itself.

For details on each of the feed access interfaces, visit their respective reference pages.

Document Tags and Contributors

 Contributors to this page: teoli, trevorh, MarkFinkle, Ptak82, Mgjbot, Chbok, Nukeador, Sheppy, Nickolay
 Last updated by: trevorh,