我需要在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:
看看 MDN 上的select
DOM 事件。
一旦释放鼠标或键(至少在Chrome 40中(,它就会触发。
document.addEventListener('select', callback);