Копирование в буфер обмена/ рефакторинг
Всем привет есть вот такой код для копирования в буфер обмена обычного тескового поля, но почему то очень сильно тупит, поиогите отрифакторить может станет чуть быстрее!
const copyData = (event) => {
let element = event.target;
if (element.tagName == "SPAN" || element.tagName == "BUTTON" ) {
element = element.querySelector("svg");
}
if (element.tagName !== "svg") {
return;
}
let text = element.dataset.article;
if (window.isSecureContext && navigator.clipboard) {
navigator.clipboard.writeText(text).then(
() => {
// console.log("Скопировано: " + text);
},
(err) => {
console.log("Не удалось скопировать данные. " + err);
});
} else {
if (copyToClipboard(element, text)) {
console.log("Скопировано: " + text);
};
}
}
async function copyToClipboard(text) { await
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(text);
} else {
const textArea = document.createElement("textarea");
textArea.value = text;
textArea.style.position = "absolute";
textArea.style.left = "-999999px";
document.body.prepend(textArea);
textArea.select();
try {
document.execCommand('copy');
} catch (error) {
console.error(error);
} finally {
textArea.remove();
}
}