Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在chrome extension中侦听所选文本拖动事件_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 在chrome extension中侦听所选文本拖动事件

Javascript 在chrome extension中侦听所选文本拖动事件,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我希望我的扩展在选择(突出显示)某个文本然后拖动时侦听事件。 就像打开新标签一样,将url拖动到标签框。我已经看到了这个答案,但它在单击图标时会突出显示文本,但我希望我的某些函数foo()在选中并拖动文本时自动启动。有人能帮我吗。首先,您需要创建处理程序函数: function highlightHandler(e) { // get the highlighted text var text = document.getSelection(); // check if

我希望我的扩展在选择(突出显示)某个文本然后拖动时侦听事件。
就像打开新标签一样,将url拖动到标签框。我已经看到了这个答案,但它在单击图标时会突出显示文本,但我希望我的某些函数foo()在选中并拖动文本时自动启动。有人能帮我吗。

首先,您需要创建处理程序函数:

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);
    }
}
然后,您需要将其绑定到文档:

document.onmouseup = highlightHandler;
最后,编写您的
doStuff
函数来执行您希望它执行的操作:

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

干净的代码和良好的评论,正是我所寻找的。