Skip to content

Instantly share code, notes, and snippets.

@imjasonh
Created February 20, 2016 17:30
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save imjasonh/c6028a9a3a3157724f7e to your computer and use it in GitHub Desktop.
Save imjasonh/c6028a9a3a3157724f7e to your computer and use it in GitHub Desktop.
Code snippet to copy text to clipboard (doesn't work from a Chrome extension background.js)
var text = 'Hello world';
var buffer = document.createElement('buffer');
document.body.appendChild(buffer);
buffer.value = text;
buffer.focus();
buffer.selectionStart = 0;
buffer.selectionEnd = buffer.value.length;
if (!document.execCommand('copy')) {
console.log('Couldn\'t copy from buffer');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment