Getting selected text in a Firefox extension
I’m developing a Firefox extension for a client which does something with the currently selected text in the browser window.
The standard way to get the selection is with
Selection is empty in Gmail
An alternative way to getSelection
The standard Search Google for “whatever” contextual menu item does work in Gmail, so obviously it gets the selection another way.
I found a function
getBrowserSelection() in the
browser.js file in Firefox’s chrome. It is used by Firefox for the contextual menu search.
This is how it gets the selection:
var focusedWindow = document.commandDispatcher.focusedWindow; var selection = focusedWindow.getSelection();
I don’t know what the difference is, but I am now using this code in my Firefox extension and it is working well.