- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试创建一个简单的 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/
我正在尝试创建一个简单的 Thunderbird 扩展,它在回复消息时对 plain/text 消息进行操作。对当前编辑器的内容应用修改后,我需要将光标放在特定位置,因为现在光标总是放在回复的末尾。
我是一名优秀的程序员,十分优秀!