mirror of
https://github.com/songquanpeng/message-pusher.git
synced 2025-09-27 04:26:31 +08:00
feat: add fallback for copy function (#135)
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user