Created
February 4, 2014 23:51
-
-
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/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
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...
doesn't work.. FF 48
Thanks all!
Does the same technique works for copy image into clipbard?
@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
Hey, I think I have an even cleaner version of this, which a) uses an input instead of a div, and b) keeps the element invisible on the page: