8

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?

bjoern
  • 283
  • 3
  • 9

3 Answers3

12

I found the following solution:

  1. Set up a manifest file to define a content script that is added to every page, and a separate background page.
  2. In the Content Script .js file, add an event listener for the 'copy' event, either for the document or the window. This event listener is called whenever the user initiates a copy action.
  3. Since content scripts exist in a security sandbox (e.g., no cross-site XMLHttpRequests), we probably want to respond to the event in the background page. To do so, use the Chrome message passing API so send a message to the background page.

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({});
  });
bjoern
  • 283
  • 3
  • 9
  • 3
    In case you've found this answer 9 years too late (like me), then [a few updates to the API are required](https://stackoverflow.com/a/13710044/630530). – Wayne Smallman Jun 27 '19 at 09:52
0

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.

Kinlan
  • 16,315
  • 5
  • 56
  • 88
  • Thanks, that is a good start for detecting copy events executed via keyboard shortcuts. I'll likely need a different mechanism for detecting Edit menu->copy invocations, outside Javascript. – bjoern May 25 '10 at 22:56
0

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

Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90
  • Great find - I was not aware of the experimental API. Unfortunately, it looks like this API only enables the extension to *execute* a copy. I am looking for a way to be *notified* when the user initiates a copy. – bjoern May 25 '10 at 23:39
  • You can't do that with extensions – BrunoLM May 25 '10 at 23:55
  • To execute copy/paste/cut in extensions, you use the clipboard API as I mentioned above. If you want to listen when the user initiates a copy, you cannot do that with extensions. Since the clipboard API is experimental, you can send your concerns why listening for copy events is needed. You can send your email to the chromium-extensions@chromium.org group: http://groups.google.com/a/chromium.org/group/chromium-extensions/topics – Mohamed Mansour May 26 '10 at 18:21