Google Chrome 扩展:如何检测复制操作(Ctrl-C 和编辑复制)?
如何检测用户已在 Google Chrome 扩展程序的当前活动选项卡中选择并复制了某些内容?
似乎没有合适的事件来处理 chrome.tabs 或 chrome.windows 中的剪贴板。
有没有办法通过内容脚本检测此类操作?
How might I detect that a user has selected and copied some content in the currently active tab in a Google Chrome Extension?
It appears that there are no suitable Events that deal with the Clipboard in chrome.tabs or chrome.windows.
Is there a way to detect such actions through Content Scripts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我找到了以下解决方案:
一个小的工作示例:
manifest.json
oncopy.js
background.html
I found the following solution:
A small working example:
manifest.json
oncopy.js
background.html
您必须使用自己的事件处理程序捕获文档元素上的 onkeypress,检查 event.keyCode = 'C' (或 ascii 代码)以及 event.ctrlKey = true
如果这是有效的,那么很可能是用户复制了当前页面上的文本。
您还需要捕获 tab.updated 事件,以便知道何时绑定 onkeypress 事件。
You will have to capture onkeypress on the document element using your own event Handler, check that event.keyCode = 'C' (or the ascii code) and that the event.ctrlKey = true
If this is valid, then it is likely that the user copied text on the current page.
You will need to also capture the tab.updated event so that you know when to bind the onkeypress event.
Google Chrome 扩展程序有一个实验性 API 供您使用,它允许您执行复制、剪切和粘贴。它目前存在于测试版本中,所以很快,它将从实验状态转变为稳定状态。
http://code.google.com/chrome/extensions/dev/实验.剪贴板.html
Google Chrome Extensions has an experimental API for you to use that will allow you to execute a copy, cut, and paste. It currently exists in the beta build, so very soon, it will be out of experimental into stable.
http://code.google.com/chrome/extensions/dev/experimental.clipboard.html