17 lines
521 B
JavaScript
17 lines
521 B
JavaScript
export async function copyText(text) {
|
|
if (navigator.clipboard && window.isSecureContext) {
|
|
await navigator.clipboard.writeText(text);
|
|
return;
|
|
}
|
|
const input = document.createElement("textarea");
|
|
input.value = text;
|
|
input.setAttribute("readonly", "");
|
|
input.style.position = "fixed";
|
|
input.style.opacity = "0";
|
|
document.body.appendChild(input);
|
|
input.select();
|
|
const copied = document.execCommand("copy");
|
|
input.remove();
|
|
if (!copied) throw new Error("Nie udało się skopiować linku");
|
|
}
|