【问题标题】:Getting Thunderbird's email editor object from a restartless (bootstrapped) addon从无需重启(引导)的插件中获取 Thunderbird 的电子邮件编辑器对象
【发布时间】:2017-01-19 07:54:22
【问题描述】:

我正在为 Thunderbird 创建一个无需重启(引导)的插件,以复制我多年前制作的基于覆盖的插件的功能。希望是操纵新电子邮件正文中的一些文本。在the old overlay version 中,它非常简单:我可以只使用 gMsgCompose 对象。如何在无需重启的插件中获取撰写窗口的 gMsgCompose 对象?

这是我现在的代码。有一段时间我认为 edElem 几乎是我想要的,这就是为什么我要测试转储 edElem.editortype(当我确实有正确的对象时,它应该是“textmail”或“htmlmail”)。

    'use strict';

    const Cc = Components.classes;
    const Ci = Components.interfaces;
    const Cu = Components.utils;

    Cu.import("resource://gre/modules/Services.jsm");

    function startup(data, reason)
    {
        Services.wm.addListener(MyAddon.windowListener);
    }

    function shutdown(data, reason)
    {
        Services.wm.removeListener(MyAddon.windowListener);
    }

    function install(aData, aReason) { }
    function uninstall(aData, aReason) { }

    if(!MyAddon) var MyAddon = {};
    MyAddon.windowListener = {
        onOpenWindow: function(aXULWindow) {
            let aDOMWindow = aXULWindow
                .QueryInterface(Ci.nsIInterfaceRequestor)
                .getInterface(Ci.nsIDOMWindow);
            if (!aDOMWindow) return;

            aDOMWindow.addEventListener("load", function _cl_load(event){
                aDOMWindow.removeEventListener("load", _cl_load, false);
                MyAddon.handleOpenWindow(aDOMWindow, event.target);
            }, false);
        }
    }; // MyAddon.windowListener

    MyAddon.handleOpenWindow = function(aDOMWindow, DOMdocument) {
        if(DOMdocument.documentURI != "chrome://messenger/content/messengercompose/messengercompose.xul")
            return;
        let edWindow = DOMdocument.documentElement;
        // Just to be sure
        if(edWindow.id != 'msgcomposeWindow')
            return;

        let edElem = DOMdocument.getElementById("content-frame");

        /* I thought edElem was the element I want, but the following
         * returns an empty result, which I take to mean the element isn't
         * fully loaded yet.
         */
        dump("XXX handleOpenWindow: edElem.editortype="+edElem.editortype+"\n");

        /* Adding a listener to this element doesn't get the behaviour
         * I want either. I've tried hooking several events
         * in the following fashion, and the event handler code doesn't
         * execute for any of them, so maybe edElem isn't what I want.
         */
        [ "load",
          "unload",
          "compose-window-init",
          "compose-window-close",
          "compose-fields-ready",
          "compose-send-message",
        ].forEach(function(eventName) {
            edElem.addEventListener(eventName, function _eE_FIXME(event){
                dump("XXX "+eventName+": edElem.editortype="+edElem.editortype+"\n");
            }, false);
        });

        /* So, how do I get control of the editor,
         * so I can read and play with its content?
         */

    }; // MyAddon.handleOpenWindow

我在 stackoverflow 上发现的唯一一个接近我想要的问题是Get sender and recipients in Thunderbird extension upon sending message。我已经尝试过他们在那里所做的事情,但它似乎所做的只是给了我另一种获取整个撰写窗口的方法,我已经可以得到了。

【问题讨论】:

    标签: javascript xul thunderbird thunderbird-addon


    【解决方案1】:

    我在onOpenWindow 中制作了一个带有计时器的丑陋存根:

    domWindow.setTimeout( function() { uploadHook( domWindow ); }, 500 );
    

    那么(在uploadHook)我可以得到编辑器:

    var editorNode = domWindow.document.getElementById("content-frame");
    // GetCurrentEditor() equivalent:
    nsIEditor = editorNode.getEditor(editorNode.contentWindow)
    

    但正在寻找更好的解决方案。

    【讨论】:

    • @Jaap:问题是一样的。我什至有一个答案(可能对其他人有帮助),但我现在认为这不是最好的答案。我应该删除它吗?
    • 我被你的最后一句话误导了,这首先表明你正在寻找答案。我将删除我的 cmets,您只需保持原样(或使用更好的解决方案对其进行改进 ;-))
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-05-12
    • 2010-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 1970-01-01
    相关资源
    最近更新 更多