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