Javascript:返回Chrome控制台中突出显示的所有文字[重复]

问题描述 投票:0回答:1

这个问题在这里已有答案:

我需要在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;
     }
}

哪个工作一次,所以有一个很好的方法来迭代这个,所以每次我突出它打印我突出显示的东西?

javascript google-chrome
1个回答
0
投票

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

看看select上的MDN DOM事件。

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

document.addEventListener('select', callback);

© www.soinside.com 2019 - 2024. All rights reserved.