2015-03-12 30 views
1

我正在嘗試創建一個簡單的Thunderbird擴展,它在回覆郵件時對plain/text消息進行操作。在對當前編輯器的內容進行修改後,我需要將光標放在特定的位置上,因爲現在光標始終放在回覆的末尾。如何在Thunderbird擴展中通過javascript在nsIEditor/nsIPlaintextEditor中設置光標位置?

這可能嗎?我幾乎到處搜索,但沒有找到解決辦法。 我在這裏花了幾個小時:

關於該插件的幾句話。該插件執行一項簡單工作,它在回覆時刪除引用消息中的所有「空白」行。我的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 Plugins的新手,所以任何幫助將不勝感激。我應該閱讀哪些內容,在哪裏可以找到完整且有效的文檔,例子等。

在此先感謝。

+2

程序員不接受有關實現代碼或查找文檔,示例或其他資源的問題。 – 2015-03-12 23:59:18

+0

我已標記爲遷移到StackOverflow。 – 2015-03-13 00:00:25

+0

好的,很抱歉,謝謝你移植我的問題。 – 2015-03-13 00:04:16

回答

2

我目前正在創建一個擴展,所以我面臨類似的問題。說實話,在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/Rangehttps://developer.mozilla.org/en-US/docs/Web/API/Selection

隨着range.setStart()range.setEnd()您可以定義一個指針必須是。上面的例子將光標設置到被操縱文本的末尾。 但我不確定removeAllranges()是否會導致問題。

相關問題