Skip to content

Commit 2d8cca3

Browse files
authored
refactor: delete clipboardjs (#2727)
text copying can be done without using additional libraries
1 parent cf7fec1 commit 2d8cca3

File tree

7 files changed

+40
-47
lines changed

7 files changed

+40
-47
lines changed

web/assets/clipboard/clipboard.min.js

-7
This file was deleted.

web/assets/js/util/common.js

+18
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,24 @@ function formatSecond(second) {
6464
}
6565
}
6666

67+
function copyToClipboard(text) {
68+
// !! here old way of copying is used because not everyone can afford https connection
69+
return new Promise((resolve) => {
70+
const textarea = document.createElement("textarea");
71+
72+
textarea.value = text;
73+
74+
document.body.appendChild(textarea);
75+
76+
textarea.select();
77+
document.execCommand("copy");
78+
79+
document.body.removeChild(textarea);
80+
81+
resolve(text)
82+
})
83+
}
84+
6785
function addZero(num) {
6886
if (num < 10) {
6987
return "0" + num;

web/html/common/qrcode_modal.html

+6-11
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('qrCode-sub',genSubLink(qrModal.client.subId))" id="qrCode-sub" class="qr-cv"></canvas>
13+
<canvas @click="copyToClipboard(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('qrCode-subJson',genSubJsonLink(qrModal.client.subId))" id="qrCode-subJson" class="qr-cv"></canvas>
21+
<canvas @click="copyToClipboard(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('qrCode-'+index, row.link)" :id="'qrCode-'+index" class="qr-cv"></canvas>
30+
<canvas @click="copyToClipboard(row.link)" :id="'qrCode-'+index" class="qr-cv"></canvas>
3131
</tr-qr-bg>
3232
</tr-qr-box>
3333
</template>
@@ -41,7 +41,6 @@
4141
dbInbound: new DBInbound(),
4242
client: null,
4343
qrcodes: [],
44-
clipboard: null,
4544
visible: false,
4645
subId: '',
4746
show: function(title = '', dbInbound, client) {
@@ -79,14 +78,10 @@
7978
qrModal: qrModal,
8079
},
8180
methods: {
82-
copyToClipboard(elementId, content) {
83-
this.qrModal.clipboard = new ClipboardJS('#' + elementId, {
84-
text: () => content,
85-
});
86-
this.qrModal.clipboard.on('success', () => {
81+
copyToClipboard(content) {
82+
return copyToClipboard(content).then(() => {
8783
app.$message.success('{{ i18n "copied" }}')
88-
this.qrModal.clipboard.destroy();
89-
});
84+
})
9085
},
9186
setQrCode(elementId, content) {
9287
new QRious({

web/html/common/text_modal.html

+7-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
:href="'data:application/text;charset=utf-8,' + encodeURIComponent(txtModal.content)"
88
:download="txtModal.fileName">[[ txtModal.fileName ]]
99
</a-button>
10-
<a-button type="primary" id="copy-btn">{{ i18n "copy" }}</a-button>
10+
<a-button type="primary" @click="txtModal.copy(txtModal.content)">{{ i18n "copy" }}</a-button>
1111
</template>
1212
<a-input style="overflow-y: auto;" type="textarea" v-model="txtModal.content"
1313
:autosize="{ minRows: 10, maxRows: 20}"></a-input>
@@ -20,24 +20,18 @@
2020
content: '',
2121
fileName: '',
2222
qrcode: null,
23-
clipboard: null,
2423
visible: false,
2524
show: function (title = '', content = '', fileName = '') {
2625
this.title = title;
2726
this.content = content;
2827
this.fileName = fileName;
2928
this.visible = true;
30-
textModalApp.$nextTick(() => {
31-
if (this.clipboard === null) {
32-
this.clipboard = new ClipboardJS('#copy-btn', {
33-
text: () => this.content,
34-
});
35-
this.clipboard.on('success', () => {
36-
app.$message.success('{{ i18n "copied" }}')
37-
this.close();
38-
});
39-
}
40-
});
29+
},
30+
copy: function (content = '') {
31+
copyToClipboard(content).then(() => {
32+
app.$message.success('{{ i18n "copied" }}')
33+
this.close();
34+
})
4135
},
4236
close: function () {
4337
this.visible = false;

web/html/xui/inbound_info_modal.html

+9-14
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" id="copy-sub-link" @click="copyToClipboard('copy-sub-link', infoModal.subLink)"></a-button>
261+
<a-button size="small" icon="snippets" @click="copyToClipboard(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" id="copy-subJson-link" @click="copyToClipboard('copy-subJson-link', infoModal.subJsonLink)"></a-button>
270+
<a-button size="small" icon="snippets" @click="copyToClipboard(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" id="copy-tg-link" @click="copyToClipboard('copy-tg-link', infoModal.clientSettings.tgId)"></a-button>
282+
<a-button size="small" icon="snippets" @click="copyToClipboard(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" :id="'copy-url-link-'+index" @click="copyToClipboard('copy-url-link-'+index, link.link)"></a-button>
293+
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copyToClipboard(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" :id="'copy-url-link-'+index" @click="copyToClipboard('copy-url-link-'+index, link.link)"></a-button>
307+
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copyToClipboard(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" :id="'copy-url-link-'+index" @click="copyToClipboard('copy-url-link-'+index, infoModal.links[index])"></a-button>
434+
<a-button style="min-width: 24px;" size="small" icon="snippets" @click="copyToClipboard(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">
@@ -464,7 +464,6 @@
464464
clientStats: [],
465465
upStats: 0,
466466
downStats: 0,
467-
clipboard: null,
468467
links: [],
469468
index: null,
470469
isExpired: false,
@@ -533,14 +532,10 @@
533532
},
534533
},
535534
methods: {
536-
copyToClipboard(elementId, content) {
537-
this.infoModal.clipboard = new ClipboardJS('#' + elementId, {
538-
text: () => content,
539-
});
540-
this.infoModal.clipboard.on('success', () => {
535+
copyToClipboard(content) {
536+
return copyToClipboard(content).then(() => {
541537
app.$message.success('{{ i18n "copied" }}')
542-
this.infoModal.clipboard.destroy();
543-
});
538+
})
544539
},
545540
statsColor(stats) {
546541
return usageColor(stats.up + stats.down, app.trafficDiff, stats.total);

web/html/xui/inbounds.html

-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,6 @@
546546
{{template "js" .}}
547547
<script src="{{ .base_path }}assets/base64/base64.min.js"></script>
548548
<script src="{{ .base_path }}assets/qrcode/qrious2.min.js?{{ .cur_ver }}"></script>
549-
<script src="{{ .base_path }}assets/clipboard/clipboard.min.js?{{ .cur_ver }}"></script>
550549
<script src="{{ .base_path }}assets/uri/URI.min.js?{{ .cur_ver }}"></script>
551550
<script src="{{ .base_path }}assets/js/model/inbound.js?{{ .cur_ver }}"></script>
552551
<script src="{{ .base_path }}assets/js/model/dbinbound.js?{{ .cur_ver }}"></script>

web/html/xui/index.html

-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,6 @@
332332
</a-modal>
333333
</a-layout>
334334
{{template "js" .}}
335-
<script src="{{ .base_path }}assets/clipboard/clipboard.min.js?{{ .cur_ver }}"></script>
336335
{{template "component/themeSwitcher" .}}
337336
{{template "textModal"}}
338337
<script>

0 commit comments

Comments
 (0)