User:Clarkbw/STEEL Examples

From MozillaWiki
Jump to: navigation, search
Draft-template-image.png THIS PAGE IS A WORKING DRAFT Pencil-emoji U270F-gray.png
The page may be difficult to navigate, and some information on its subject might be incomplete and/or evolving rapidly.
If you have any questions or ideas, please add them as a new topic on the discussion page.

Here are your instructions

  1. Grab the STEEL extension for thunderbird from bug 408370
  2. Install the STEEL Extension
  3. Write Extensions that use the STEEL API

I recommend using the extension wizard to create your base extension, then copy / paste the example code.

Also checkout the Thunderbird Extensions HowTo for more tips, especially the How do I use an SQLite Databse in Thunderbird

Here are some working examples of code using the STEEL API.

Watch for New Mail

Here's a snippet that listens for new mail arriving on all accounts and folders, then prints details of each new message to the javascript console.

var steelyournewmail = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("steelyournewmail-strings");

    // FIXME: currently we have to add our listener for new mail arriving in all folders
    for (var i = 0; i < Application.accounts.all.length; i++) {
      Application.console.log("looking in account: " + Application.accounts.all[i].displayName);
      for (var j = 0; j < Application.accounts.all[i].folders.length; j++) {
        Application.console.log("looking in folder: " + Application.accounts.all[i].folders[j].name);
        Application.accounts.all[i].folders[j].events.addListener("newMail", steelyournewmailListener);
      }
    }
  },
};

var steelyournewmailListener = {
  handleEvent : function (event) {
    var msg = event.data;
    Application.console.log("handleEvent: " + event.type); // newMail
    Application.console.log("handleEvent (msg.id): " + msg.id);
    Application.console.log("handleEvent (msg.from): " + msg.from);
    Application.console.log("handleEvent (msg.to): " + msg.to);
    Application.console.log("handleEvent (msg.cc): " + msg._xpcomMsg.ccList);
    Application.console.log("handleEvent (msg.date): " + msg.date);
    Application.console.log("handleEvent (msg.subject): " + msg.subject);
    Application.console.log("handleEvent (msg.unread): " + msg.unread);
    Application.console.log("handleEvent (msg.flagged): " + msg.flagged);
    Application.console.log("handleEvent (msg.priority): " + msg.priority);
    Application.console.log("handleEvent (msg._xpcomMsg): " + msg._xpcomMsg);
  },
};

window.addEventListener("load", function(e) { steelyournewmail.onLoad(e); }, false);

The old version of watching for new mail still works

Watch for Sent Mail

Hopefully soon STEEL APIs will give access to folder types and we won't have to compare for a folder name like "Sent"

var steelyoursentmail = {
  onLoad: function() {
    // initialization code
    this.initialized = true;
    this.strings = document.getElementById("steelyoursentmail-strings");

    for (var i = 0; i < Application.accounts.all.length; i++) {
      for (var j = 0; j < Application.accounts.all[i].folders.length; j++) {
        if ( Application.accounts.all[i].folders[j].name == "Sent" ) {
          Application.console.log("looking in folder: " + Application.accounts.all[i].folders[j].name);
          Application.accounts.all[i].folders[j].events.addListener("newMail", steelyoursentmailListener);
        }
      }
    }
  },
};

var steelyoursentmailListener = {
  handleEvent : function (event) {
    var msg = event.data;
    Application.console.log("handleEvent: " + event.type); // newMail
    Application.console.log("handleEvent (msg.id): " + msg.id);
    Application.console.log("handleEvent (msg.to): " + msg.to);
    Application.console.log("handleEvent (msg.cc): " + msg._xpcomMsg.ccList);
    Application.console.log("handleEvent (msg.date): " + msg.date);
    Application.console.log("handleEvent (msg.subject): " + msg.subject);
    // Now we could file this into different directories
  },
};

window.addEventListener("load", function(e) { steelyoursentmail.onLoad(e); }, false);

List all Accounts

If you'd like to Iterate through all your accounts, here's how you can do that and grab all the information from those accounts.

    for (var i = 0; i < Application.accounts.all.length; i++) {
      Application.console.log("Account.id: " + Application.accounts.all[i].id);
      Application.console.log("Account.address: " + Application.accounts.all[i].address);
      Application.console.log("Account.displayName: " + Application.accounts.all[i].displayName);
      Application.console.log("Account.folder: " + Application.accounts.all[i].folders);
      Application.console.log("Account.type: " + Application.accounts.all[i].type);
      Application.console.log("Account.folders: " + Application.accounts.all[i].folders);
    }

Watch for Folders Being Added or Removed

First we iterate through all the accounts setting up our folder listener. As a new folder is added or remove we can receive the event and work with the folder object.

// attach listener to each account
for (var i = 0; i < Application.accounts.all.length; i++) {
  Application.accounts.all[i].events.addListener("folderAdded", steelFolderAddedListener)
  Application.accounts.all[i].events.addListener("folderRemoved", steelFolderRemovedListener)
}

var steelFolderAddedListener = {
  handleEvent : function (event) {
    var folder = event.data;
    Application.console.log("Folder Added: " + folder.name);
  },
};

var steelFolderRemovedListener = {
  handleEvent : function (event) {
    var folder = event.data;
    Application.console.log("Folder Removed: " + folder.name);
  },
};

Watch for Sub-Folders Added / Removed

This code, unlike the folder watch above, only watches the current folders for sub-folders being created. It will not be notified when top level folders for the account are created.

for (var i = 0; i < Application.accounts.all.length; i++) {
  for (var j = 0; j < Application.accounts.all[i].folders.length; j++) {
    Application.accounts.all[i].folders[j].events.addListener("folderAdded", steelFolderAddedListener);
    Application.accounts.all[i].folders[j].events.addListener("folderRemoved", steelFolderRemovedListener);
  }
}

var steelFolderAddedListener = {
  handleEvent : function (event) {
    var folder = event.data;
    Application.console.log("Folder Added: " + folder.name);
  },
};

var steelFolderRemovedListener = {
  handleEvent : function (event) {
    var folder = event.data;
    Application.console.log("Folder Removed: " + folder.name);
  },
};

List All Folders

This snippet loops through all your accounts and their folders listing their available information. CAREFUL! if you have accounts with lots of emails the allMsgs function can take a while to finish.

    for (var i = 0; i < Application.accounts.all.length; i++) {
      for (var j = 0; j < Application.accounts.all[i].folders.length; j++) {
        Application.console.log("Folder.name: " + Application.accounts.all[i].folders[j].name);
        Application.console.log("Folder.subFolders: " + Application.accounts.all[i].folders[j].subFolders);
        Application.console.log("Folder.unreadMsgs: " + Application.accounts.all[i].folders[j].unreadMsgs);
        Application.console.log("Folder.allMsgs: " + Application.accounts.all[i].folders[j].allMsgs);
//        Application.accounts.all[i].folders[j].markAsRead();
//        Application.accounts.all[i].folders[j].compact();
//        Application.accounts.all[i].folders[j].addSubFolder("folder");
//        Application.accounts.all[i].folders[j].deleteSubFolder("folder");
      }
    }

Watch for Messages Being Shown

This will send us a signal for every message as it is shown in the message pane view.

 Application.events.addListener("messageShow", steelmessageShowListener);

Our listener function handles the messageShow signal. Unfortunately right now bug 424203 prevents us from grabbing the event.data and using it as a message object. However we can still examine the message in the message pane.

 var steelmessageShowListener = {
   handleEvent : function (event) {
     // FIXME: for now we can just assume the message has rendered in the message pane
     var linkNodes = document.getElementById('messagepane').contentDocument.links;
     for (var index = 0; index < linkNodes.length; index++)
       Application.console.log("link: " + linkNodes[index].href);
   },
 };