1

I am new to Google Extension programming.

I faced with a problem - how to get the coordinates of selected region? I am building some sort of translation app and when user select word, pop-up should appear nearby with word information.

Currently, I am getting selection via chrome.contextMenus.create with "onclick" event. (like Get selected text in a chrome extension). And I assume to use chrome.tabs.executeScript to run my pop-up code, but how to get selection coodinates (x, y) to position my pop-up?

Thank you for suggestion.

Community
  • 1
  • 1
  • Well what have you attempted thus far? – pattmorter Jul 30 '14 at 15:01
  • I have no idea where to look. function genericOnClick(info, tab) gives info about selection without coordinates and tab - all information about current tab. I google chrome API, but found nothing. I have an idea - to inject at the page js and get selection coordinates from there, but I think there should be more nice mechanism for it. – user3365404 Jul 30 '14 at 15:06
  • Have you considered assigning a general `onClick` event to the body of the extension (or whatever you are getting the event from) with jQuery, and then capturing the mouse cursor location at the time of the click? Let me put an example together for you and answer the question. – pattmorter Jul 30 '14 at 15:11

1 Answers1

3

So the easiest thing I have found to do is to poll the mouse location but only capture it when a click is also occurring.

$(document).ready(function(){
  $(document).mousemove(function(e){
    $('html').click(function(e) {
      $('#c_loc').html("" + e.pageX + ", " + e.pageY);
    });
  });
});

Note that I use html and not body. Body only captures the body height (which you might want, your discretion) but i used html because my example is only 100 or so pixels high. Aka i needed a larger click space for use of example.

See this example.

pattmorter
  • 991
  • 9
  • 21