feat: add fallback for copy function (#135)

This commit is contained in:
Johnson Sun
2024-11-10 10:51:24 +08:00
committed by GitHub
parent fe8638324f
commit 30cd8ec528

View File

@@ -19,8 +19,31 @@ export function isRoot() {
export async function copy(text) {
let okay = true;
try {
await navigator.clipboard.writeText(text);
} catch (e) {
if (navigator.clipboard){
await navigator.clipboard.writeText(text);
} else {
var textarea = document.createElement("textarea");
textarea.value = text;
textarea.style.opacity = 0;
textarea.style.position = "absolute";
textarea.style.left = "-9999px";
document.body.appendChild(textarea);
var range = document.createRange();
range.selectNode(textarea);
window.getSelection().removeAllRanges();
window.getSelection().addRange(range);
document.execCommand("copy");
document.body.removeChild(textarea);
window.getSelection().removeAllRanges();
}
}
catch (e) {
okay = false;
console.error(e);
}