javascript - 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;
}
}
哪个工作一次,那么有没有一种很好的方法来迭代它,所以每次我突出显示它时都会打印我突出显示的内容?
解决方案
来自https://stackoverflow.com/a/28425297/2553191:
看看MDNselect
上的DOM 事件。
一旦释放鼠标或键(至少在 Chrome 40 中),它就会触发。
document.addEventListener('select', callback);
推荐阅读
- c# - Xunit 测试测试资源管理器未运行测试
- java - 通知访问允许但切换未更改
- c++ - 是否可以在 C++ 中创建自己的宏以供 QMake 稍后解决?
- ios - 为什么 url.bookmarkData 返回 nil?
- javascript - 具有二维数组的角度复选框
- visual-studio - 如何在颤动/飞镖扩展中禁用蓝色警告下划线标志?
- c - 如果子线程被套接字读取阻塞,如何通过管道通知主线程?
- python - 为什么 for 循环中定义的子图表现不一致?('numpy.ndarray' 对象没有属性'plot')
- php - Laravel SQLSTATE[HY000] [1049] 未知数据库 + PHP 工匠迁移
- javascript - 向下滑动汉堡菜单