首页 > 解决方案 > Javascript:返回 Chrome 控制台中突出显示的所有文本

问题描述

我需要在我用鼠标突出显示的 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;
     }
}

哪个工作一次,那么有没有一种很好的方法来迭代它,所以每次我突出显示它时都会打印我突出显示的内容?

标签: javascriptgoogle-chrome

解决方案


来自https://stackoverflow.com/a/28425297/2553191

看看MDNselect上的DOM 事件。

一旦释放鼠标或键(至少在 Chrome 40 中),它就会触发。

document.addEventListener('select', callback);


推荐阅读