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
}
干净的代码和良好的评论,正是我所寻找的。