Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Clipboard data in Chrome Extension?

Tags:

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.

like image 760
schumacherj Avatar asked Mar 28 '14 01:03

schumacherj


People also ask

How do I find clipboard data in chrome?

To find it, open a new tab, paste chrome://flags into Chrome's Omnibox and then press the Enter key. Search for “Clipboard” in the search box.

What does the clipboard API enable an extension to do?

clipboard API is provided to allow users to access data of the clipboard. This is a temporary solution for chromeos platform apps until open-web alternative is available.

How can I copy text from clipboard in chrome?

2. Right click and select "Copy" or press on CTRL+C (Windows) / CMD+C (MAC). 3. Click on the Easy Clipboard extension to view saved text snippets!


1 Answers

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.

like image 72
Ivan Nevostruev Avatar answered Sep 24 '22 04:09

Ivan Nevostruev