Thunderbird 扩展,加载事件似乎只发生一次

发布于 2024-12-04 15:17:17 字数 803 浏览 1 评论 0原文

我正在尝试编写一个扩展来自动填充外发消息的主题行。

下面的代码似乎只执行一次。它链接到 chrome://messenger/content/messengercompose/messengercompose.xul 的覆盖层,当用户点击写入按钮撰写新消息时,该覆盖层会打开。我第一次单击“写入”并在“收件人”字段中输入文本时,主题行会自动填充。但是,如果我关闭“撰写”窗口并打开一个新窗口,则“收件人”字段中不会有已注册的事件侦听器。

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);

在过去的四天里,我一直在试图解决这个问题,但就是无法解决。我对 javascript 和 DOM 没有太多的经验(主要是 java),所以我认为这对于你们中的一些专家来说可能很容易弄清楚。请帮忙。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2024-12-11 15:17:17

好吧,我已经开始工作了。我只花了大约一周的时间进行研究,但这不就是我们喜欢编码的原因吗?不管怎样,解决方案似乎是使用 gMsgCompose、状态侦听器和 compose-window-init 事件(而不是 load 事件)。所以就是这样。

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);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文