| 1 |
|
| 2 |
jQuery(document).ready(function () { |
| 3 |
document.getElementById("wpvr-copy-shortcode").addEventListener("click", function() { |
| 4 |
copyToClipboard(document.getElementById("copy-shortcode")); |
| 5 |
}); |
| 6 |
|
| 7 |
function copyToClipboard(elem) { |
| 8 |
// create hidden text element, if it doesn\'t already exist |
| 9 |
var targetId = "_hiddenCopyText_"; |
| 10 |
var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA"; |
| 11 |
var origSelectionStart, origSelectionEnd; |
| 12 |
if (isInput) { |
| 13 |
// can just use the original source element for the selection and copy |
| 14 |
target = elem; |
| 15 |
origSelectionStart = elem.selectionStart; |
| 16 |
origSelectionEnd = elem.selectionEnd; |
| 17 |
} else { |
| 18 |
// must use a temporary form element for the selection and copy |
| 19 |
target = document.getElementById(targetId); |
| 20 |
if (!target) { |
| 21 |
var target = document.createElement("textarea"); |
| 22 |
target.style.position = "absolute"; |
| 23 |
target.style.left = "-9999px"; |
| 24 |
target.style.top = "0"; |
| 25 |
target.id = targetId; |
| 26 |
document.body.appendChild(target); |
| 27 |
} |
| 28 |
target.textContent = elem.textContent; |
| 29 |
} |
| 30 |
// select the content |
| 31 |
var currentFocus = document.activeElement; |
| 32 |
target.focus(); |
| 33 |
target.setSelectionRange(0, target.value.length); |
| 34 |
|
| 35 |
// copy the selection |
| 36 |
var succeed; |
| 37 |
try { |
| 38 |
succeed = document.execCommand("copy"); |
| 39 |
document.getElementById("wpvr-copied-notice").innerHTML = "Copied!"; |
| 40 |
} catch(e) { |
| 41 |
succeed = false; |
| 42 |
} |
| 43 |
// restore original focus |
| 44 |
if (currentFocus && typeof currentFocus.focus === "function") { |
| 45 |
currentFocus.focus(); |
| 46 |
} |
| 47 |
|
| 48 |
setTimeout(function(){ |
| 49 |
document.getElementById("wpvr-copied-notice").innerHTML = ""; |
| 50 |
}, 2000 ); |
| 51 |
|
| 52 |
if (isInput) { |
| 53 |
// restore prior selection |
| 54 |
elem.setSelectionRange(origSelectionStart, origSelectionEnd); |
| 55 |
} else { |
| 56 |
// clear temporary content |
| 57 |
target.textContent = ""; |
| 58 |
} |
| 59 |
document.getElementById("wpvr-copy-shortcode").scrollIntoView() |
| 60 |
return succeed; |
| 61 |
}; |
| 62 |
}); |
| 63 |
|