Get sender and recipients in Thunderbird extension upon sending message

前端 未结 2 857
我寻月下人不归
我寻月下人不归 2021-01-27 03:04

I\'m playing around trying to create a Thunderbird extension, one of the bootstrapped/restartless type (I mean, javascript code is not run from overlays. Instead listen

相关标签:
2条回答
  • 2021-01-27 03:30

    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.

    0 讨论(0)
  • 2021-01-27 03:44

    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
    
    0 讨论(0)
提交回复
热议问题