Skip to content

Commit 26bf693

Browse files
committed
refactor: move copy function to utils.js
1 parent 7483fb2 commit 26bf693

File tree

6 files changed

+63
-43
lines changed

6 files changed

+63
-43
lines changed

web/assets/js/util/common.js

-18
Original file line numberDiff line numberDiff line change
@@ -53,24 +53,6 @@ function formatSecond(second) {
5353
}
5454
}
5555

56-
function copyToClipboard(text) {
57-
// !! here old way of copying is used because not everyone can afford https connection
58-
return new Promise((resolve) => {
59-
const textarea = document.createElement("textarea");
60-
61-
textarea.value = text;
62-
63-
document.body.appendChild(textarea);
64-
65-
textarea.select();
66-
document.execCommand("copy");
67-
68-
document.body.removeChild(textarea);
69-
70-
resolve(text)
71-
})
72-
}
73-
7456
function addZero(num) {
7557
if (num < 10) {
7658
return "0" + num;

web/assets/js/util/utils.js

+32
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,38 @@ class Wireguard {
480480
}
481481
}
482482

483+
class ClipboardManager {
484+
static copyText(content = "") {
485+
// !! here old way of copying is used because not everyone can afford https connection
486+
return new Promise((resolve) => {
487+
try {
488+
const textarea = window.document.createElement('textarea');
489+
490+
textarea.style.fontSize = '12pt';
491+
textarea.style.border = '0';
492+
textarea.style.padding = '0';
493+
textarea.style.margin = '0';
494+
textarea.style.position = 'absolute';
495+
textarea.style.left = '-9999px';
496+
textarea.style.top = `${window.pageYOffset || document.documentElement.scrollTop}px`;
497+
textarea.setAttribute('readonly', '');
498+
textarea.value = content;
499+
500+
window.document.body.appendChild(textarea);
501+
502+
textarea.select();
503+
window.document.execCommand("copy");
504+
505+
window.document.body.removeChild(textarea);
506+
507+
resolve(true)
508+
} catch {
509+
resolve(false)
510+
}
511+
})
512+
}
513+
}
514+
483515
class Base64 {
484516
static encode(content = "", safe = false) {
485517
if (safe) {

web/html/common/qrcode_modal.html

+9-7
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
<a-tag color="purple" class="qr-tag"><span>{{ i18n "pages.settings.subSettings"}}</span></a-tag>
1111
<tr-qr-bg class="qr-bg-sub">
1212
<tr-qr-bg-inner class="qr-bg-sub-inner">
13-
<canvas @click="copyToClipboard(genSubLink(qrModal.client.subId))" id="qrCode-sub" class="qr-cv"></canvas>
13+
<canvas @click="qrModal.copy(genSubLink(qrModal.client.subId))" id="qrCode-sub" class="qr-cv"></canvas>
1414
</tr-qr-bg-inner>
1515
</tr-qr-bg>
1616
</tr-qr-box>
1717
<tr-qr-box class="qr-box">
1818
<a-tag color="purple" class="qr-tag"><span>{{ i18n "pages.settings.subSettings"}} Json</span></a-tag>
1919
<tr-qr-bg class="qr-bg-sub">
2020
<tr-qr-bg-inner class="qr-bg-sub-inner">
21-
<canvas @click="copyToClipboard(genSubJsonLink(qrModal.client.subId))" id="qrCode-subJson" class="qr-cv"></canvas>
21+
<canvas @click="qrModal.copy(genSubJsonLink(qrModal.client.subId))" id="qrCode-subJson" class="qr-cv"></canvas>
2222
</tr-qr-bg-inner>
2323
</tr-qr-bg>
2424
</tr-qr-box>
@@ -27,7 +27,7 @@
2727
<tr-qr-box class="qr-box">
2828
<a-tag color="green" class="qr-tag"><span>[[ row.remark ]]</span></a-tag>
2929
<tr-qr-bg class="qr-bg">
30-
<canvas @click="copyToClipboard(row.link)" :id="'qrCode-'+index" class="qr-cv"></canvas>
30+
<canvas @click="qrModal.copy(row.link)" :id="'qrCode-'+index" class="qr-cv"></canvas>
3131
</tr-qr-bg>
3232
</tr-qr-box>
3333
</template>
@@ -78,10 +78,12 @@
7878
qrModal: qrModal,
7979
},
8080
methods: {
81-
copyToClipboard(content) {
82-
return copyToClipboard(content).then(() => {
83-
app.$message.success('{{ i18n "copied" }}')
84-
})
81+
copy(content) {
82+
ClipboardManager
83+
.copyText(content)
84+
.then(() => {
85+
app.$message.success('{{ i18n "copied" }}')
86+
})
8587
},
8688
setQrCode(elementId, content) {
8789
new QRious({

web/html/common/text_modal.html

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@
2828
this.visible = true;
2929
},
3030
copy: function (content = '') {
31-
copyToClipboard(content).then(() => {
32-
app.$message.success('{{ i18n "copied" }}')
33-
this.close();
34-
})
31+
ClipboardManager
32+
.copyText(content)
33+
.then(() => {
34+
app.$message.success('{{ i18n "copied" }}')
35+
this.close();
36+
})
3537
},
3638
close: function () {
3739
this.visible = false;

web/html/xui/inbound_info_modal.html

+12-10
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@
258258
<tr-info-title class="tr-info-title">
259259
<a-tag color="purple">Subscription Link</a-tag>
260260
<a-tooltip title='{{ i18n "copy" }}'>
261-
<a-button size="small" icon="snippets" @click="copyToClipboard(infoModal.subLink)"></a-button>
261+
<a-button size="small" icon="snippets" @click="copy(infoModal.subLink)"></a-button>
262262
</a-tooltip>
263263
</tr-info-title>
264264
<a :href="[[ infoModal.subLink ]]" target="_blank">[[ infoModal.subLink ]]</a>
@@ -267,7 +267,7 @@
267267
<tr-info-title class="tr-info-title">
268268
<a-tag color="purple">Json Link</a-tag>
269269
<a-tooltip title='{{ i18n "copy" }}'>
270-
<a-button size="small" icon="snippets" @click="copyToClipboard(infoModal.subJsonLink)"></a-button>
270+
<a-button size="small" icon="snippets" @click="copy(infoModal.subJsonLink)"></a-button>
271271
</a-tooltip>
272272
</tr-info-title>
273273
<a :href="[[ infoModal.subJsonLink ]]" target="_blank">[[ infoModal.subJsonLink ]]</a>
@@ -279,7 +279,7 @@
279279
<tr-info-title class="tr-info-title">
280280
<a-tag color="blue">[[ infoModal.clientSettings.tgId ]]</a-tag>
281281
<a-tooltip title='{{ i18n "copy" }}'>
282-
<a-button size="small" icon="snippets" @click="copyToClipboard(infoModal.clientSettings.tgId)"></a-button>
282+
<a-button size="small" icon="snippets" @click="copy(infoModal.clientSettings.tgId)"></a-button>
283283
</a-tooltip>
284284
</tr-info-title>
285285
</tr-info-row>
@@ -290,7 +290,7 @@
290290
<tr-info-title class="tr-info-title">
291291
<a-tag class="tr-info-tag" color="green">[[ link.remark ]]</a-tag>
292292
<a-tooltip title='{{ i18n "copy" }}'>
293-
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copyToClipboard(link.link)"></a-button>
293+
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copy(link.link)"></a-button>
294294
</a-tooltip>
295295
</tr-info-title>
296296
<code>[[ link.link ]]</code>
@@ -304,7 +304,7 @@
304304
<tr-info-title class="tr-info-title">
305305
<a-tag class="tr-info-tag" color="green">[[ link.remark ]]</a-tag>
306306
<a-tooltip title='{{ i18n "copy" }}'>
307-
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copyToClipboard(link.link)"></a-button>
307+
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copy(link.link)"></a-button>
308308
</a-tooltip>
309309
</tr-info-title>
310310
<code>[[ link.link ]]</code>
@@ -431,7 +431,7 @@
431431
<tr-info-title class="tr-info-title">
432432
<a-tag color="blue">Config</a-tag>
433433
<a-tooltip title='{{ i18n "copy" }}'>
434-
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copyToClipboard(infoModal.links[index])"></a-button>
434+
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copy(infoModal.links[index])"></a-button>
435435
</a-tooltip>
436436
</tr-info-title>
437437
<div v-html="infoModal.links[index].replaceAll(`\n`,`<br />`)" style="border-radius: 1rem; padding: 0.5rem;" class="client-table-odd-row">
@@ -532,10 +532,12 @@
532532
},
533533
},
534534
methods: {
535-
copyToClipboard(content) {
536-
return copyToClipboard(content).then(() => {
537-
app.$message.success('{{ i18n "copied" }}')
538-
})
535+
copy(content) {
536+
ClipboardManager
537+
.copyText(content)
538+
.then(() => {
539+
app.$message.success('{{ i18n "copied" }}')
540+
})
539541
},
540542
statsColor(stats) {
541543
return usageColor(stats.up + stats.down, app.trafficDiff, stats.total);

web/html/xui/inbounds.html

+4-4
Original file line numberDiff line numberDiff line change
@@ -883,7 +883,7 @@
883883
this.exportSubs(dbInbound.id);
884884
break;
885885
case "clipboard":
886-
this.copyToClipboard(dbInbound.id);
886+
this.copy(dbInbound.id);
887887
break;
888888
case "resetTraffic":
889889
this.resetTraffic(dbInbound.id);
@@ -1382,9 +1382,9 @@
13821382
}
13831383
txtModal.show('{{ i18n "pages.inbounds.export"}}', copyText.join('\r\n'), 'All-Inbounds');
13841384
},
1385-
copyToClipboard(dbInboundId) {
1386-
dbInbound = this.dbInbounds.find(row => row.id === dbInboundId);
1387-
txtModal.show('{{ i18n "pages.inbounds.inboundData" }}', JSON.stringify(dbInbound, null, 2));
1385+
copy(dbInboundId) {
1386+
dbInbound = this.dbInbounds.find(row => row.id === dbInboundId);
1387+
txtModal.show('{{ i18n "pages.inbounds.inboundData" }}', JSON.stringify(dbInbound, null, 2));
13881388
},
13891389
async startDataRefreshLoop() {
13901390
while (this.isRefreshEnabled) {

0 commit comments

Comments
 (0)