这个问题在这里已有答案:
我需要在Chrome页面中获取所有用鼠标突出显示的文本,我对javascript知之甚少,但我发现了这个:
var text = "";
if (window.getSelection){
text = window.getSelection().toString();
console.log(text)
} else if (document.selection && document.selection.type !=
"Control") {
text = document.selection.createRange().text;
}
}
哪个工作一次,所以有一个很好的方法来迭代这个,所以每次我突出它打印我突出显示的东西?
来自https://stackoverflow.com/a/28425297/2553191:
看看select
上的MDN DOM事件。
一旦释放鼠标或键,它就会触发(至少在Chrome 40中)。
document.addEventListener('select', callback);