简体   繁体   English

在chrome扩展中收听选定的文本拖动事件

[英]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. 我已经看到了这个答案这个答案,但是当点击图标时它会突出显示文本但是我希望我的一些函数foo()在选择和拖动文本时自动触发。 can any one help me please. 任何人都可以帮助我。

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: 最后,编写doStuff函数来执行您希望它执行的操作:

function doStuff(text) {
    // do something cool
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM