The first step is to find all the Elements that intersect the position. To do this, you must use the Element.getBoundingClientRect method. Then you can easily get all the Text nodes that are inside this Element . The hard part is figuring out what text inside these Text nodes is within these boundaries. You can start with Element.getClientRects to get lines of text inside Element . Here is a great example .
Then you want to build a Range object by changing startOffset and endOffset . You can use the getClientRects method for a Range object.
source share