deft-utils
Version:
deft utils are designed to speed up your development
39 lines (34 loc) • 1.06 kB
JavaScript
function fallbackCopyTextToClipboard(text, verbose) {
var textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position="fixed"; //avoid scrolling to bottom
document.body.appendChild(textArea);
textArea.focus();
textArea.select();
if (verbose) {
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Fallback: Copying text command was ' + msg);
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
}
}
document.body.removeChild(textArea);
}
function copyTextToClipboard(text, verbose) {
if (!navigator.clipboard) {
fallbackCopyTextToClipboard(text, verbose);
return;
}
navigator.clipboard.writeText(text).then(function() {
if (verbose) {
console.log('Async: Copying to clipboard was successful!');
}
}, function(err) {
if (verbose) {
console.error('Async: Could not copy text: ', err);
}
});
}
module.exports = copyTextToClipboard;