I’m writing a chrome application that injects js code to a page, when the user selects any text a popup is shown next to the text.
I’m using the following code to receive the coordinates of the selected text:
var rect = window.getSelection().getRangeAt(0).getBoundingClientRect();
var bottom = rect.bottom + document.body.scrollTop;
var left = rect.left + document.body.scrollLeft
it works great except for when the user selects some text inside an input area, when he does the bottom-left bounding rect coordinates are (0,0) and my popup is shown at the top left corner of the page.
Any ideas how to solve it? I need the solution to work only in chrome.
The code below is an excact copy of the function at this answer. For an explanation of the code, have a look at the linked answer. Demo: http://jsfiddle.net/56Rep/5/