2

In Thunderbird's message composer, I need to use javascript to see if the user has selected any text, and optionally get that selected text.

I tried this:

var thisselection = window.getSelection();
alert("selection = " + thisselection.toString() ); 

But even if text is selected, it says nothing is selected. I am sure I don't understand what is going on. I was reading from MDN.

I've also tried:

var editor = gMsgCompose.editor; 
var thisselection = editor.getSelection.toString();

but then I get an error saying getSelection is not a function to be used with editor.

bgmCoder
  • 6,205
  • 8
  • 58
  • 105

2 Answers2

1

Ah, found it:

var thisselection = document.commandDispatcher.focusedWindow.getSelection();
var thistext = thisselection.toString();
alert(thistext);
bgmCoder
  • 6,205
  • 8
  • 58
  • 105
0

Another way (not so magic as commandDispatcher):

var editor = document.getElementById("content-frame");
var edocument = editor.contentDocument;
var sel = edocument.getSelection();
Valerij
  • 71
  • 1
  • 3