2

I'm trying to create a simple Thunderbird extension which operates on plain/text messages when replying to a message. After applying modifications to current editor's content I need to place the cursor on a specific position, because now the cursor is always placed on the end of the reply.

Is this possible? I searched almost everywhere, but did not find a solution. I've spent hours here:

Few words about the plugin. The plugin does one simple job, it removes all "empty" lines from the quoted message when replying. A simplified version of my JavaScript file:

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

I'm a newbie when it comes to Thunderbird Plugins, so any help will be appreciated. What should I read, where can I find a complete and valid documentation, examples etc.

Thanks in advance.

Community
  • 1
  • 1
Thomas Szteliga
  • 402
  • 4
  • 15

1 Answers1

2

I'm currently creating an extension too, so i faced a similar problem. To be honest, the mdn doc is a real disappointment

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

For more information, i recommend to read this https://developer.mozilla.org/en-US/docs/Web/API/Range and this https://developer.mozilla.org/en-US/docs/Web/API/Selection

With range.setStart() and range.setEnd() you can define where the cursor has to be. The example above will set the cursor right to the end of the manipulated text. But i'm not sure if removeAllranges() can cause issues.

volpe
  • 46
  • 5