Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firefox Extension: Get selected text

I am working on a simple Firefox Extension and I want to get the selected text. I tried this:

var WordCount = {
    /* ... */
    changeSelected: function() {
        var selectedText = this.getSelection();
        var words = this.countWords(selectedText);
        this.changeStatus(words, " selected");
        //alert(selectedText);
    },
    getSelection: function(e) {
        var focused_window = document.commandDispatcher.focusedWindow;
        var sel_text = focused_window.getSelection();
        return sel_text.toString();    
    }
}
window.addEventListener("select", function(e) { WordCount.changeSelected(); }, false);

The Problem is, that I dont get the selection with document.commandDispatcher.focusedWindow.getSelection() and I don't know why :(

like image 994
f00860 Avatar asked Jul 16 '09 15:07

f00860


2 Answers

Your problem is that document.commandDispatcher.focusedWindow is going to be pointing to a chrome window, where I suspect you actually want a content window. Try replacing that with content.getSelection()

like image 172
sdwilsh Avatar answered Nov 12 '22 23:11

sdwilsh


This works in firefox javascripting, so should be OK

window.getSelection().toString();

My guess is that document.commandDispatcher.focusedWindow fails

like image 1
David Snabel-Caunt Avatar answered Nov 13 '22 00:11

David Snabel-Caunt