Bia пре 2 година
родитељ
комит
815d3d1a7d
1 измењених фајлова са 15 додато и 2 уклоњено
  1. 15 2
      src/libs/clipboard.lib.ts

+ 15 - 2
src/libs/clipboard.lib.ts

@@ -1,8 +1,21 @@
 
 export function useClipboard() {
   function copy(text: string) {
-    return navigator.clipboard.writeText(text)
+    if (navigator.clipboard && window.isSecureContext) {
+      return navigator.clipboard.writeText(text)
+    } else {
+      const textArea = document.createElement("textarea");
+      textArea.value = text
+      textArea.style.position = "absolute";
+      textArea.style.opacity = '0';
+      document.body.appendChild(textArea);
+      textArea.select();
+      return new Promise((resolve, reject) => {
+        document.execCommand("copy") ? resolve('') : reject(new Error("出错了"));
+        textArea.remove()
+      })
+    }
   }
-
+  
   return { copy }
 }