From a824cdc4979c9b70c05b37804d3f9dc2b547d0a6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E6=9E=AB=E8=90=BD=E6=97=B6?= <rugtee@outlook.com>
Date: Thu, 21 Nov 2024 11:54:27 +0800
Subject: [PATCH] =?UTF-8?q?[fix]=E5=A4=8D=E5=88=B6=E6=BF=80=E6=B4=BB?=
 =?UTF-8?q?=E7=A0=81=E9=A1=B5=E9=9D=A2=E6=BB=9A=E5=8A=A8=E5=88=B0=E5=BA=95?=
 =?UTF-8?q?=E9=83=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 src/main/resources/static/js/index.js | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/src/main/resources/static/js/index.js b/src/main/resources/static/js/index.js
index 5ef82a9..56ded80 100644
--- a/src/main/resources/static/js/index.js
+++ b/src/main/resources/static/js/index.js
@@ -74,16 +74,22 @@ $(document).ready(function() {
             return navigator.clipboard.writeText(val);
         } else {
             console.log(val);
+            const scrollX = window.scrollX;
             const textArea = document.createElement('textarea')
             textArea.value = val
             // 使text area不在viewport,同时设置不可见
             document.body.appendChild(textArea)
             textArea.focus()
             textArea.select()
-            return new Promise((res, rej) => {
-                document.execCommand('copy') ? res() : rej()
-                textArea.remove()
-            })
+            try {
+                const result = document.execCommand('copy');
+                return result ? Promise.resolve() : Promise.reject();
+            } catch (e) {
+                return Promise.reject(e);
+            } finally {
+                textArea.remove();
+                window.scrollTo(scrollX, 0);
+            }
         }
     };