Get sender and recipients in Thunderbird extension upon sending message

[亡魂溺海] 提交于 2019-12-02 06:08:12

I've done it even more simply using this.

var win = Services.wm.getMostRecentWindow("msgcompose");
composeFields = {};
win.Recipients2CompFields(composeFields); 
// composeFields has more properties than this like cc and bcc but the
// below is what you asked for.
Components.utils.reportError(composeFields.to);  // Debug output.
var sender = document.getElementById("msgIdentity").description

Well, thanks to the help from people on this mozilla.dev.apps.thunderbird thread and this mozilla.dev.extensions thread, I'm able to access both sender and recipients from within compose-send-message event listener.

Here's the relevant code... actually, a bit more: the juice is just within onComposeSendMessage:

var windowMediator = components.classes['@mozilla.org/appshell/window-mediator;1'].
  getService(components.interfaces.nsIWindowMediator);

var listener = new Listener(windowMediator);
windowMediator.addListener(listener);

function Listener (windowMediator) {

  var self = this;
  var _mediator = windowMediator;
  var _compose = null;

  this.onOpenWindow = function (aWindow) {
    // [...]

    _compose = aWindow.docShell.
      QueryInterface(components.interfaces.nsIInterfaceRequestor).
      getInterface(components.interfaces.nsIDOMWindow);

    _compose.addEventListener('compose-send-message', self.onComposeSendMessage, true);
  };

  this.onComposeSendMessage = function (event) {
    event.currentTarget.removeEventListener(event.type, self.onComposeSendMessage, true);

    // event.currentTarget.gMsgCompose <--> _compose.gMsgCompose

    // Get sender
    log(_compose.gMsgCompose.identity.email); // DEBUG with custom log function

    // Get recipients
    log(_compose.gMsgCompose.compFields.to);  // DEBUG
    log(_compose.gMsgCompose.compFields.cc);  // DEBUG
    log(_compose.gMsgCompose.compFields.bcc);  // DEBUG
  };

  this.onCloseWindow = function () {
    _mediator.removeListener(self); // Remove itself, so to not receive message twice
  };

  // [...]

}

Again, thanks all on those groups for feedbacks.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!