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?
I found the following solution:
A small working example:
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({});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With