Pregunta

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.

¿Fue útil?

Solución

Ah, found it:

var thisselection = document.commandDispatcher.focusedWindow.getSelection();
var thistext = thisselection.toString();
alert(thistext);

Otros consejos

Another way (not so magic as commandDispatcher):

var editor = document.getElementById("content-frame");
var edocument = editor.contentDocument;
var sel = edocument.getSelection();
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top