javascript - 如何通过 Thunderbird 扩展中的 javascript 设置 nsIEditor/nsIPlaintextEditor 中的光标位置?

标签 javascript plugins xul thunderbird-addon

我正在尝试创建一个简单的 Thunderbird 扩展,该扩展在回复消息时对纯文本消息进行操作。对当前编辑器的内容进行修改后,我需要将光标放置在特定位置,因为现在光标始终放置在回复的末尾。

这可能吗?我几乎到处寻找,但没有找到解决方案。 我在这里呆了几个小时:

关于该插件的几句话。该插件做了一项简单的工作,它在回复时从引用的消息中删除所有“空”行。我的 JavaScript 文件的简化版本:

var myStateListener = {
    init: function (e) {
        gMsgCompose.RegisterStateListener(myStateListener);
    },
    NotifyComposeFieldsReady: function () {
        // alter message fields here
    },
    NotifyComposeBodyReady: function () {
        // alter message body here
        try {
            var editor = GetCurrentEditor();
            var editor_type = GetCurrentEditorType();
            editor.beginTransaction();
            editor.beginningOfDocument();
            // plain text editor
            if (editor_type == "textmail" || editor_type == "text") {
                var content = editor.outputToString('text/plain', 4);
                var contentArray = content.split(/\r?\n/);
                var contentArrayLength = contentArray.length;
                var newContentArray = [];
                for (var i = 0; i < contentArrayLength; i++) {
                    // match "non-empty" lines
                    if (!/^>{1,} *$/.test(contentArray[i])) {
                        // Add non-matching rows
                        newContentArray.push(contentArray[i]);
                    }
                }
                // join array of newContent lines
                newContent = newContentArray.join("\r\n");
                // select current content
                editor.selectAll();
                // replace selected editor content with cleaned-up content
                editor.insertText(newContent);
            } else {
                // HTML messages not handled yet
                void(null);
            }
            editor.endTransaction();
        } catch (ex) {
            Components.utils.reportError(ex);
            return false;
        }
    },
    ComposeProcessDone: function (aResult) {
        //
    },
    SaveInFolderDone: function (folderURI) {
        //
    }
};
//
window.addEventListener("compose-window-init", myStateListener.init, true);

我是 Thunderbird 插件的新手,因此我们将不胜感激。我应该阅读什么,在哪里可以找到完整且有效的文档、示例等。

提前致谢。

最佳答案

我目前也在创建一个扩展,所以我遇到了类似的问题。说实话,mdn 文档确实令人失望

var sr = editor.selection.getRangeAt(0).cloneRange(); // old cursor
var range = editor.document.createRange(); // prepare new cursor
// do stuff to the editor
range.setStart(sr.startContainer, editor.selection.focusNode.textContent.length);
range.setEnd(sr.startContainer, editor.selection.focusNode.textContent.length);
editor.selection.removeAllRanges();
editor.selection.addRange(range);

有关更多信息,我建议阅读此内容 https://developer.mozilla.org/en-US/docs/Web/API/Range和这个 https://developer.mozilla.org/en-US/docs/Web/API/Selection

使用 range.setStart()range.setEnd() 您可以定义光标所在的位置。上面的示例将光标设置在所操作文本的末尾。 但我不确定 removeAllranges() 是否会导致问题。

关于javascript - 如何通过 Thunderbird 扩展中的 javascript 设置 nsIEditor/nsIPlaintextEditor 中的光标位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29040814/

相关文章:

javascript - 合并/最小化多个 javascript document.body.innerHTML.replace

javascript - 从命令行使用 headless chrome 执行单个 Javascript 文件

eclipse - 如何让 Scala 编译器插件在 Scala IDE 中工作

javascript - 如何在纯javascript中手动触发按键事件时设置键码值?

javascript - 我可以在脚本中完全禁用 'debugger' 吗?

android - 无法在 unity java 插件上使用 google play 服务

ios - 如何在 Nativescript 上访问 ios 核心音频(AudioQueue)

javascript - 如何从 firefox 浏览器中将选定的文本写为图像

tinymce - 如何为 XUL 应用程序使用富文本编辑器

javascript - 如何在 Firefox 扩展中更改状态栏面板的背景颜色