1 | var copyTextToClipboard = function (input) {
|
2 | var el = document.createElement('textarea');
|
3 |
|
4 | el.value = input;
|
5 |
|
6 |
|
7 | el.setAttribute('readonly', '');
|
8 |
|
9 | el.style.contain = 'strict';
|
10 | el.style.position = 'absolute';
|
11 | el.style.left = '-9999px';
|
12 | el.style.fontSize = '12pt';
|
13 |
|
14 | var selection = getSelection();
|
15 | var originalRange = false;
|
16 | if (selection.rangeCount > 0) {
|
17 | originalRange = selection.getRangeAt(0);
|
18 | }
|
19 |
|
20 | document.body.appendChild(el);
|
21 | el.select();
|
22 |
|
23 |
|
24 | el.selectionStart = 0;
|
25 | el.selectionEnd = input.length;
|
26 |
|
27 | var success = false;
|
28 | try {
|
29 | success = document.execCommand('copy');
|
30 | } catch (err) {}
|
31 |
|
32 | document.body.removeChild(el);
|
33 |
|
34 | if (originalRange) {
|
35 | selection.removeAllRanges();
|
36 | selection.addRange(originalRange);
|
37 | }
|
38 |
|
39 | return success;
|
40 | };
|
41 |
|
42 | export default copyTextToClipboard;
|