gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 17:00:09 25 4
gpt4 key购买 nike

我正在尝试创建一个简单的 Thunderbird 扩展,它在回复消息时对 plain/text 消息进行操作。对当前编辑器的内容应用修改后,我需要将光标放在特定位置,因为现在光标总是放在回复的末尾。

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

关于插件的几句话。该插件做了一项简单的工作,它在回复时从引用的消息中删除所有“空”行。我的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com