我正在尝试创建一个简单的 Thunderbird 扩展,该扩展在回复消息时对纯文本
消息进行操作。对当前编辑器的内容进行修改后,我需要将光标放置在特定位置,因为现在光标始终放置在回复的末尾。
这可能吗?我几乎到处寻找,但没有找到解决方案。 我在这里呆了几个小时:
- https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/NsIEditor
- http://mdn.beonex.com/en/Extensions/Thunderbird/HowTos/Common_Thunderbird_Use_Cases/Compose_New_Message.html#Modify_Message_Body
- 以及其他人(我需要至少 10 个声誉才能发布 2 个以上链接)。
关于该插件的几句话。该插件做了一项简单的工作,它在回复时从引用的消息中删除所有“空”行。我的 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/