| 1 |
window.CTC = (function (window, document, navigator) { |
| 2 |
var textArea, |
| 3 |
copy; |
| 4 |
|
| 5 |
function isOS() { |
| 6 |
return navigator.userAgent.match(/ipad|iphone/i); |
| 7 |
} |
| 8 |
|
| 9 |
function createTextArea(text) { |
| 10 |
textArea = document.createElement('textArea'); |
| 11 |
textArea.value = text; |
| 12 |
document.body.appendChild(textArea); |
| 13 |
} |
| 14 |
|
| 15 |
function selectText() { |
| 16 |
var range, |
| 17 |
selection; |
| 18 |
|
| 19 |
if (isOS()) { |
| 20 |
range = document.createRange(); |
| 21 |
range.selectNodeContents(textArea); |
| 22 |
selection = window.getSelection(); |
| 23 |
selection.removeAllRanges(); |
| 24 |
selection.addRange(range); |
| 25 |
textArea.setSelectionRange(0, 999999); |
| 26 |
} else { |
| 27 |
textArea.select(); |
| 28 |
} |
| 29 |
} |
| 30 |
|
| 31 |
function copyToClipboard() { |
| 32 |
document.execCommand('copy'); |
| 33 |
document.body.removeChild(textArea); |
| 34 |
|
| 35 |
// Redirect to page. |
| 36 |
if( copyTheCode.redirect_url ) { |
| 37 |
window.location.href = copyTheCode.redirect_url; |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
copy = function (text) { |
| 42 |
createTextArea(text); |
| 43 |
selectText(); |
| 44 |
copyToClipboard(); |
| 45 |
} |
| 46 |
|
| 47 |
copySelection = function ( source ) { |
| 48 |
const selection = window.getSelection() |
| 49 |
const range = document.createRange() |
| 50 |
range.selectNodeContents( source[0] ) |
| 51 |
selection.removeAllRanges() |
| 52 |
selection.addRange(range) |
| 53 |
document.execCommand('copy') |
| 54 |
selection.removeAllRanges() |
| 55 |
} |
| 56 |
|
| 57 |
return { |
| 58 |
copy: copy, |
| 59 |
copySelection: copySelection, |
| 60 |
}; |
| 61 |
})(window, document, navigator); |