Browse Source

feat: add fallback for copy function (#135)

Johnson Sun 11 months ago
parent
commit
30cd8ec528
1 changed files with 25 additions and 2 deletions
  1. 25 2
      web/src/helpers/utils.js

+ 25 - 2
web/src/helpers/utils.js

@@ -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);
   }