-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
copy.js
28 lines (27 loc) · 768 Bytes
/
copy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
function copyToClipboard(strToCopy) {
navigator.permissions.query({ name: "clipboard-write" }).then((result) => {
if (result.state == "granted" || result.state == "prompt") {
navigator.clipboard.writeText(strToCopy).then(
function () {
// ignore and digest
},
function () {
return fallBackCopy(strToCopy);
}
);
} else {
return fallBackCopy(strToCopy);
}
});
}
function fallBackCopy(strToCopy) {
const el = document.createElement("textarea");
el.value = strToCopy;
el.setAttribute("readonly", "");
el.style.position = "absolute";
el.style.left = "-9999px";
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
}