4

I want my extension to listen to the event when a some text is selected (highlighted) and then dragged. just like opening new tab with dragging url to tab box. I have seen this answer this answer but it gets the text highlighted when icon is clicked but I want my some function foo() to fire automatically when text is selected and dragged. can any one help me please.

Community
  • 1
  • 1
user1476394
  • 337
  • 4
  • 9

1 Answers1

11

So first, you'll want to create your handler function:

function highlightHandler(e) {
    // get the highlighted text
    var text = document.getSelection();
    // check if anything is actually highlighted
    if(text !== '') {
        // we've got a highlight, now do your stuff here
        doStuff(text);
    }
}

And then, you'll need to bind it to your document:

document.onmouseup = highlightHandler;

And finally, write your doStuff function to do what you want it to do:

function doStuff(text) {
    // do something cool
}
Bluefire
  • 13,519
  • 24
  • 74
  • 118