I want to be able to get the x,y coordinates of the end of a text string that has been highlighted in a web browser. I tried searching online for something similar and found that Quora does almost exactly what I want. Their code is obsfucated/ compressed though so I can't work out what they did.
To see any example visit a question on Quora (such as this: http://www.quora.com/Job-Interviews/Whats-the-craziest-thing-you-ever-said-at-an-interview-and-still-got-the-job) and highlight some text in an answer. A little box will pop-up that says 'Embed Quote'. Getting the position of the embedded quote is what I want to do.
I've made a little demo that grabs the mouse position - but I'd like it to be more accurate if possible. There's a jsfiddle of the code here: http://jsfiddle.net/BinaryMoon/zy8hY/
$(function(){
$(document.body).bind('mouseup', function(e){
var selection;
if (window.getSelection) {
selection = window.getSelection();
} else if (document.selection) {
selection = document.selection.createRange();
}
if ( selection.toString().length > 3 ) {
var tip = $('<div class="pointer" />');
var p =
tip.css({
'position':'absolute',
'left': e.pageX,
'top': e.pageY
});
tip.appendTo('.post');
}
});
$(document.body).bind('mousedown', function(e){
$('.pointer').remove();
});
});