Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save joeperrin-gists/8814825 to your computer and use it in GitHub Desktop.

Select an option

Save joeperrin-gists/8814825 to your computer and use it in GitHub Desktop.
Copy To Clipboard in Google Chrome Extensions using Javascript. Source: http://www.pakzilla.com/2012/03/20/how-to-copy-to-clipboard-in-chrome-extension/
function copyToClipboard( text ){
var copyDiv = document.createElement('div');
copyDiv.contentEditable = true;
document.body.appendChild(copyDiv);
copyDiv.innerHTML = text;
copyDiv.unselectable = "off";
copyDiv.focus();
document.execCommand('SelectAll');
document.execCommand("Copy", false, null);
document.body.removeChild(copyDiv);
@oscarqht

Copy link
Copy Markdown

Does the same technique works for copy image into clipbard?

@nameldk

nameldk commented Jul 11, 2018

Copy link
Copy Markdown

@dannyid const input = document.createElement('textarea'); It's would be better. The textarea will keep the raw format and the input will lose format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment