UNPKG

1.15 kBJavaScriptView Raw
1(function (global, factory) {
2 typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
3 typeof define === 'function' && define.amd ? define(factory) :
4 (global.mcopy = factory());
5}(this, (function () { 'use strict';
6
7var copyTextToClipboard = function (input) {
8 var el = document.createElement('textarea');
9
10 el.value = input;
11
12 // Prevent keyboard from showing on mobile
13 el.setAttribute('readonly', '');
14
15 el.style.contain = 'strict';
16 el.style.position = 'absolute';
17 el.style.left = '-9999px';
18 el.style.fontSize = '12pt'; // Prevent zooming on iOS
19
20 var selection = getSelection();
21 var originalRange = false;
22 if (selection.rangeCount > 0) {
23 originalRange = selection.getRangeAt(0);
24 }
25
26 document.body.appendChild(el);
27 el.select();
28
29 // Explicit selection workaround for iOS
30 el.selectionStart = 0;
31 el.selectionEnd = input.length;
32
33 var success = false;
34 try {
35 success = document.execCommand('copy');
36 } catch (err) {}
37
38 document.body.removeChild(el);
39
40 if (originalRange) {
41 selection.removeAllRanges();
42 selection.addRange(originalRange);
43 }
44
45 return success;
46};
47
48return copyTextToClipboard;
49
50})));