From 26bf693dbdc2db0222ee37615e353f7ca1fdd3c1 Mon Sep 17 00:00:00 2001 From: "Shishkevich D." <135337715+shishkevichd@users.noreply.github.com> Date: Fri, 7 Mar 2025 07:27:33 +0000 Subject: [PATCH] refactor: move copy function to utils.js --- web/assets/js/util/common.js | 18 ---------------- web/assets/js/util/utils.js | 32 ++++++++++++++++++++++++++++ web/html/common/qrcode_modal.html | 16 ++++++++------ web/html/common/text_modal.html | 10 +++++---- web/html/xui/inbound_info_modal.html | 22 ++++++++++--------- web/html/xui/inbounds.html | 8 +++---- 6 files changed, 63 insertions(+), 43 deletions(-) diff --git a/web/assets/js/util/common.js b/web/assets/js/util/common.js index 3aa1549e..9ad2e01f 100644 --- a/web/assets/js/util/common.js +++ b/web/assets/js/util/common.js @@ -53,24 +53,6 @@ function formatSecond(second) { } } -function copyToClipboard(text) { - // !! here old way of copying is used because not everyone can afford https connection - return new Promise((resolve) => { - const textarea = document.createElement("textarea"); - - textarea.value = text; - - document.body.appendChild(textarea); - - textarea.select(); - document.execCommand("copy"); - - document.body.removeChild(textarea); - - resolve(text) - }) -} - function addZero(num) { if (num < 10) { return "0" + num; diff --git a/web/assets/js/util/utils.js b/web/assets/js/util/utils.js index a79ce3a0..ab7c977a 100644 --- a/web/assets/js/util/utils.js +++ b/web/assets/js/util/utils.js @@ -480,6 +480,38 @@ class Wireguard { } } +class ClipboardManager { + static copyText(content = "") { + // !! here old way of copying is used because not everyone can afford https connection + return new Promise((resolve) => { + try { + const textarea = window.document.createElement('textarea'); + + textarea.style.fontSize = '12pt'; + textarea.style.border = '0'; + textarea.style.padding = '0'; + textarea.style.margin = '0'; + textarea.style.position = 'absolute'; + textarea.style.left = '-9999px'; + textarea.style.top = `${window.pageYOffset || document.documentElement.scrollTop}px`; + textarea.setAttribute('readonly', ''); + textarea.value = content; + + window.document.body.appendChild(textarea); + + textarea.select(); + window.document.execCommand("copy"); + + window.document.body.removeChild(textarea); + + resolve(true) + } catch { + resolve(false) + } + }) + } +} + class Base64 { static encode(content = "", safe = false) { if (safe) { diff --git a/web/html/common/qrcode_modal.html b/web/html/common/qrcode_modal.html index 117dd640..914290f6 100644 --- a/web/html/common/qrcode_modal.html +++ b/web/html/common/qrcode_modal.html @@ -10,7 +10,7 @@ {{ i18n "pages.settings.subSettings"}} - + @@ -18,7 +18,7 @@ {{ i18n "pages.settings.subSettings"}} Json - + @@ -27,7 +27,7 @@ [[ row.remark ]] - + @@ -78,10 +78,12 @@ qrModal: qrModal, }, methods: { - copyToClipboard(content) { - return copyToClipboard(content).then(() => { - app.$message.success('{{ i18n "copied" }}') - }) + copy(content) { + ClipboardManager + .copyText(content) + .then(() => { + app.$message.success('{{ i18n "copied" }}') + }) }, setQrCode(elementId, content) { new QRious({ diff --git a/web/html/common/text_modal.html b/web/html/common/text_modal.html index 36589199..c15282b9 100644 --- a/web/html/common/text_modal.html +++ b/web/html/common/text_modal.html @@ -28,10 +28,12 @@ this.visible = true; }, copy: function (content = '') { - copyToClipboard(content).then(() => { - app.$message.success('{{ i18n "copied" }}') - this.close(); - }) + ClipboardManager + .copyText(content) + .then(() => { + app.$message.success('{{ i18n "copied" }}') + this.close(); + }) }, close: function () { this.visible = false; diff --git a/web/html/xui/inbound_info_modal.html b/web/html/xui/inbound_info_modal.html index f95461e2..1c561aa6 100644 --- a/web/html/xui/inbound_info_modal.html +++ b/web/html/xui/inbound_info_modal.html @@ -258,7 +258,7 @@ Subscription Link - + [[ infoModal.subLink ]] @@ -267,7 +267,7 @@ Json Link - + [[ infoModal.subJsonLink ]] @@ -279,7 +279,7 @@ [[ infoModal.clientSettings.tgId ]] - + @@ -290,7 +290,7 @@ [[ link.remark ]] - + [[ link.link ]] @@ -304,7 +304,7 @@ [[ link.remark ]] - + [[ link.link ]] @@ -431,7 +431,7 @@ Config - +
@@ -532,10 +532,12 @@ }, }, methods: { - copyToClipboard(content) { - return copyToClipboard(content).then(() => { - app.$message.success('{{ i18n "copied" }}') - }) + copy(content) { + ClipboardManager + .copyText(content) + .then(() => { + app.$message.success('{{ i18n "copied" }}') + }) }, statsColor(stats) { return usageColor(stats.up + stats.down, app.trafficDiff, stats.total); diff --git a/web/html/xui/inbounds.html b/web/html/xui/inbounds.html index 93999e97..7dc5ff5f 100644 --- a/web/html/xui/inbounds.html +++ b/web/html/xui/inbounds.html @@ -883,7 +883,7 @@ this.exportSubs(dbInbound.id); break; case "clipboard": - this.copyToClipboard(dbInbound.id); + this.copy(dbInbound.id); break; case "resetTraffic": this.resetTraffic(dbInbound.id); @@ -1382,9 +1382,9 @@ } txtModal.show('{{ i18n "pages.inbounds.export"}}', copyText.join('\r\n'), 'All-Inbounds'); }, - copyToClipboard(dbInboundId) { - dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); - txtModal.show('{{ i18n "pages.inbounds.inboundData" }}', JSON.stringify(dbInbound, null, 2)); + copy(dbInboundId) { + dbInbound = this.dbInbounds.find(row => row.id === dbInboundId); + txtModal.show('{{ i18n "pages.inbounds.inboundData" }}', JSON.stringify(dbInbound, null, 2)); }, async startDataRefreshLoop() { while (this.isRefreshEnabled) {