Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listen to selected text drag event in chrome extension

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.

like image 763
user1476394 Avatar asked Dec 19 '22 13:12

user1476394


1 Answers

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
}
like image 115
Bluefire Avatar answered Dec 24 '22 02:12

Bluefire