How to get Clipboard data in Chrome Extension?

clipboard, google-chrome, google-chrome-extension, javascript

Solution

Basically you can manipulate clipboard using `document.execCommand('paste|copy|cut')`.

You'll need to specify `"clipboardWrite"` and/or `"clipboardRead"` permissions in manifest.

"clipboardRead" Required if the extension or app uses document.execCommand('paste').

"clipboardWrite" Indicates the extension or app uses document.execCommand('copy') or document.execCommand('cut'). This permission is required for hosted apps; it's recommended for extensions and packaged apps.

Create `<input>` element (or `<textarea>`)

- Put focus to it

- Call `document.execCommand('paste')`

- Grab you string from `<input>` `value` attribute.

This worked for me to copy data to clipboard.

Problem

I'm having a hard time finding any recent info on how to add a listener for "Ctrl+C", fetching clipboard data, and then writing back to clipboard all in a Chrome Extension. All of the old code that i found was for the older versions that are now deprecated.

Original source

Related problems