I’m building a bookmarklet that takes the selected text and sends it back to my server where it is processed. It works on every site except Gmail. Anyone know how to get it to work on Gmail. Here is the code I’m using:
var selectedText = '';
if (window.getSelection) {
selectedText = window.getSelection();
} else if (document.getSelection) {
selectedText = document.getSelection();
} else if (document.selection) {
selectedText = document.selection.createRange().text;
} else {
selectedText = document.activeElement.contentWindow.getSelection();
};
I had hit the same problem and found your question looking for an answer myself.
The issue in your code, as far as I can tell, is not that window.getSelection is undefined in gmail, but simply that getSelection().toString() returns a zero length string despite text being selected. In Firefox Tim Down’s solution worked for me but not in Chrome as contentWindow in not available.
My revised code below which iterates though any frames on the page is working in Gmail for me in Firefox, Chrome and Safari. (I’ve not tested it in other browsers).