User:Clarkbw/STEEL Examples: Difference between revisions

From MozillaWiki
Jump to navigation Jump to search
(added list all accounts)
(adding watch for folders example)
Line 104: Line 104:
       Application.console.log("Account.folders: " + Application.accounts.all[i].folders);
       Application.console.log("Account.folders: " + Application.accounts.all[i].folders);
     }
     }
</pre>
== 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.
<pre>
// 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);
  },
};
</pre>
</pre>

Revision as of 23:59, 19 March 2008

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.

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

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);
  },
};