getSelection() returns empty in Google Mail
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 window.content.getSelection().
Selection is empty in Gmail
Some users reported that selected text in Gmail messages wasn’t being found by the extension. I suspect the issue is with content added using JavaScript, but I haven’t investigated.
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.