Copy text/html into clipboard using JS
npm install copy-html-to-clipboardSimple module exposing copy function that will try to use execCommand with fallback to IE-specific clipboardData interface and finally, resort to usual prompt with proper text content and message.
Forked from copy-to-clipboard to add html support.
``js
import copy from 'copy-html-to-clipboard';
copy('Text');
// Copy with options
copy('Text', {
debug: true,
message: 'Press #{key} to copy',
});
// Copy html
copy('i am bold', {
asHtml: true,
});
`
copy(text: string, options: object): boolean — tries to copy text to clipboard. Returns true if no additional keystrokes were required from user (so, execCommand, IE's clipboardData worked) or false.
|Value |Default |Notes|
|------|--------|-----|
|options.debug |false| Boolean. Optional. Enable output to console. |Boolean
|options.asHtml |false| . True - use param text as html. |Boolean
|options.onlyHtml |false| . True - if can't copy html to clipboard, don't try to copy text with alternative ways. |Boolean
|options.canUsePrompt |true| . True - try alternative ugly prompt-way. |#{key}
|options.message|Copy to clipboard: , Enter| String. Optional. Prompt message. * |
* all occurrences of #{key} are replaced with ⌘+C for macOS/iOS users, and Ctrl+C otherwise.
Works everywhere where prompt* is available. Works best (i.e. without additional keystrokes) in Chrome, FF, Safari 10+, and, supposedly, IE/Edge.
Note: does not work on some older iOS devices.
* – even though Safari 8 has prompt, you cannot specify prefilled content for prompt modal – thus it doesn't work as expected.
+ Can be used as npm package and then leveraged using commonjs bundler/loader:
``
npm i --save copy-html-to-clipboard`
+ Can be utilized using wzrd.in. Add following script to your page:html`window.copyToClipboard
You will have exposed for you to use.
```
npm i
npm test