问题
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 listeners fire for various events).
At some point I'd like to check the sender and recipients (To, Cc, Bcc) when user decides to send the message, so to perform some checks on them.
I already have a number of event listeners set up and working, including one for compose-send-message
event that gets properly fired when user confirms sending the message.
There, I'm not able to find how to get the sender email address as well as all recipients email addresses. I tried both with:
let fields = components.classes["@mozilla.org/messengercompose/composefields;1"].
createInstance(components.interfaces.nsIMsgCompFields)
and with:
let params = components.classes["@mozilla.org/messengercompose/composeparams;1"].
createInstance(components.interfaces.nsIMsgComposeParams);
let fields = params.composeFields;
but anyway fields.hasRecipients
returns false, and e.g. fields.to
is null (or empty, can't exactly recall). It looks like they're not being set by TB.
Of course I searched around, also in TB threads related to overlays extensions, but with no luck. There's a SO thread here, which does not seem to completely answer the question as it's only about the sender.
Other references: SO again, MozillaZine, TB stdlib.
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/25989864/get-sender-and-recipients-in-thunderbird-extension-upon-sending-message