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);
@misfo

misfo commented Dec 2, 2015

Copy link
Copy Markdown

Thanks, @dannyid . The input.style.position = 'fixed' "fixed" (punny, I know) an issue I had that would cause the browser to scroll.

Only thing I had to change was to use a textarea instead of an input in order to preserve line breaks...

@yairEO

yairEO commented Jun 23, 2016

Copy link
Copy Markdown

doesn't work.. FF 48

@f0r3v3ran00b

Copy link
Copy Markdown

Thanks all!

@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