Search code examples
javascriptxulthunderbirdthunderbird-addon

gDBView is not defined and xul


I'm trying to write a Thunderbird extension using XUL, a custom button that accesses the currently shown email message body and does something with it.

Apparently this would be possible using the global variable gDBView, as in the Display Mail User Agent extension:

var msgURI = null ;

if ( gDBView )
{
  msgURI = gDBView.URIForFirstSelectedMessage ;
}

if ( msgURI == null )
{
  return ;
}

var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance (Components.interfaces.nsIMessenger ) ;
var msgService = messenger.messageServiceFromURI ( msgURI ) ;

Unfortunately if in my extension I replicate the row:

msgURI = gDBView.URIForFirstSelectedMessage ;    

I get the following error from the Thunderbird console:

gDBView is not defined

Is there a reason why this happens? And is this the best (and correct) way to access a mail body?


Solution

  • You're probably running your script in the wrong context, as Wladimir correctly guessed. An easy way to check that gDBView exists is, in the menus, to hit Tools > Error Console, then paste top.opener.gDBView, then hit Enter. This returns (for me) [xpconnect wrapped (nsISupports, nsIMsgDBView, nsITreeView)] which means the object indeed does exist.