Thunderbird extension, load event seems to occur only once
I'm attempting to write an extension to autofill the subject line of an outgoing message.
The following code only seems to execute once. It's linked to an overlay of chrome://messenger/content/messengercompose/messengercompose.xul which opens when a user hits the write button to compose a new message. The first time I click write and enter text in the "to" field, the subject 开发者_开发技巧line gets autofilled. However, if I close the "compose" window and bring up a new one it will not have the registered event listener in the "to" field.
var execute = {
onLoad: function(e){
alert("onload");
var addrTextbox = document.getElementById("addressCol2#1"); //"to" field
addrTextbox.addEventListener("change", execute.autoFillSubjectLine, false);
},
autoFillSubjectLine: function(e){
var msgSubject = document.getElementById("msgSubject");
msgSubject.value = "text goes here";
},
};
window.addEventListener("load", execute.onLoad, true);
I have been trying to figure this out now for the past 4 days and just can't get it. I don't have all that much experience with javascript and the DOM, (mostly just java), so I'm thinking this might be rather easy for some of you guru folk to figure out. Please help.
Well, I got it to work. Only took me about a week of research, but isn't that why we love to code. Anyway, the solution seems to be the use of gMsgCompose, state listeners, and the compose-window-init
event as opposed to the load
event. So here it is.
var myStateListener = {
init: function(e){
gMsgCompose.RegisterStateListener(myStateListener);
},
NotifyComposeFieldsReady: function() {
},
NotifyComposeBodyReady: function() {
execute.addListener();
},
ComposeProcessDone: function(aResult) {
},
SaveInFolderDone: function(folderURI) {
}
};
var execute = {
addListener: function(){
var addrTextbox = document.getElementById("addressCol2#1"); //"to" field
addrTextbox.addEventListener("change", execute.autoFillSubjectLine, false);
},
autoFillSubjectLine: function(e){
var msgSubject = document.getElementById("msgSubject");
msgSubject.value = "text goes here";
}
};
window.addEventListener("compose-window-init", myStateListener.init, true);
精彩评论