Google Chrome 扩展程序:如何检测复制操作(Ctrl-C 和 Edit-Copy)? [英] Google Chrome Extensions: How to detect Copy action (Ctrl-C and Edit-Copy)?
本文介绍了Google Chrome 扩展程序:如何检测复制操作(Ctrl-C 和 Edit-Copy)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何检测到用户在 Google Chrome 扩展程序的当前活动标签中选择并复制了某些内容?
How might I detect that a user has selected and copied some content in the currently active tab in a Google Chrome Extension?
似乎没有合适的事件来处理 chrome.tabs 或 chrome.windows 中的剪贴板.
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?
推荐答案
我找到了以下解决方案:
I found the following solution:
- 设置清单文件以定义添加到每个页面的内容脚本和单独的背景页面.
- 在内容脚本 .js 文件中,为文档或窗口的复制"事件添加一个事件侦听器.每当用户启动复制操作时都会调用此事件侦听器.
- 由于内容脚本存在于安全沙箱中(例如,没有跨站点 XMLHttpRequests),我们可能希望响应后台页面中的事件.为此,请使用 Chrome 消息传递 API,以便向后台页面发送消息.
一个小的工作示例:
manifest.json
{
"background_page": "background.html",
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["oncopy.js"]
}
]
}
oncopy.js
// on copy event, send a message to background.html
function onCopy(e) {
chrome.extension.sendRequest({event: "copy"});
}
//register event listener for copy events on document
document.addEventListener('copy',onCopy,true);
background.html
chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) {
if (request.event == "copy") {
alert("copy detected");
}
sendResponse({});
});
这篇关于Google Chrome 扩展程序:如何检测复制操作(Ctrl-C 和 Edit-Copy)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文