-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
154 lines (137 loc) · 5.19 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Original code by Xecades
// Modified code © 2021 - 2024 YangguangZhou (GitHub: YangguangZhou)
const baseURL = "https://cards.jerryz.com.cn/api";
const icons = ["wechat", "qq", "site", "phone", "email", "github", "gitee", "coolapk", "bilibili", "zhihu", "weibo", "tiktok", "twitter", "telegram", "facebook", "instagram", "google", "microsoft", "luogu", "codeforces", "alipay", "csdn", "douban", "taobao", "linuxdo"];
function toggleBorder() {
!window.TOG &&
document.body.appendChild(
(window.TOG = document.createElement("style"))
);
(window.CHK = !window.CHK)
? (TOG.innerHTML = `*{box-shadow: 0 0 0 1px cyan}`)
: (TOG.innerHTML = "");
}
var vm = new Vue({
el: "#app",
data: {
icon: icons,
image: baseURL,
params: "",
background: "#1",
text_color: "rgba(0, 0, 0, 1)",
bg_color: "rgba(0, 0, 0, 0)",
date: new Date().getFullYear() + "-12-31",
str: "",
str_placeholder: new Date().getFullYear() + " 年末",
quote: "",
counter: "",
social: [],
debounceTimer: null,
},
watch: {
background: function (e) {
this.debouncedSetParam("img", e.slice(1));
},
params: function (e) {
this.debouncedUpdateImage();
},
text_color: function (e) {
this.debouncedSetParam("color", e.slice(5, e.length - 1).replaceAll(" ", ""));
},
bg_color: function (e) {
this.debouncedSetParam("bg", e.slice(5, e.length - 1).replaceAll(" ", ""));
},
date: function (e) {
this.debouncedSetParam("date", e);
},
str: function (e) {
this.debouncedSetParam("str", e);
},
quote: function (e) {
this.debouncedSetParam("quote", e);
},
counter: function (e) {
const regex = /^[a-zA-Z0-9_\-\.!@#$%^&*()+=\[\]{}|;:,<>?]+$/;
if (regex.test(e) || e === "") {
this.debouncedSetParam("counter", e);
} else {
alert("输入不合法!合法字符包括:英文字母、数字、下划线、连字符、点号以及常见的特殊字符(如 !@#$%^&*()+=[]{}|;:,<>?)");
this.counter = ""; // 清空输入
}
},
social: function (e) {
for (var i = 0; i < e.length; i++) this.debouncedSetParam(icons[i], e[i]);
},
},
methods: {
setParam: function (key, value) {
var tmp = new URLSearchParams(this.params);
if (!value) tmp.delete(key);
else tmp.set(key, value
.replace(/\&/gm, "{%amp%}")
.replace(/\</gm, "{%lt%}")
.replace(/\>/gm, "{%gt%}"));
this.params = tmp.toString();
},
debouncedSetParam: function(key, value) {
clearTimeout(this.debounceTimer);
this.debounceTimer = setTimeout(() => {
this.setParam(key, value);
}, 300);
},
debouncedUpdateImage: function() {
clearTimeout(this.debounceTimer);
this.debounceTimer = setTimeout(() => {
this.image = baseURL + (this.params ? "?" + this.params : "");
this.updateImageLink();
}, 300);
},
open: function () {
umami.track('generate', { open: this.image });
window.open(this.image, "_blank");
},
updateImageLink: function () {
var linkElement = document.getElementById('link');
if (linkElement) {
linkElement.textContent = this.image;
}
},
fadeIn: function (element) {
element.classList.add('fade-in');
setTimeout(function () {
element.classList.remove('fade-in');
}, 300);
},
fadeOut: function (element) {
element.classList.add('fade-out');
setTimeout(function () {
element.classList.remove('fade-out');
}, 300);
},
copyToClipboard: function () {
var linkElement = document.getElementById('link');
if (linkElement) {
navigator.clipboard.writeText(linkElement.textContent).then(function () {
this.fadeOut(linkElement);
setTimeout(function () {
linkElement.textContent = '链接已复制到剪贴板';
this.fadeIn(linkElement);
setTimeout(function () {
this.fadeOut(linkElement);
setTimeout(function () {
linkElement.textContent = this.image;
this.fadeIn(linkElement);
}.bind(this), 300);
}.bind(this), 300);
}.bind(this), 300);
}.bind(this), function (err) {
console.error('无法复制链接: ', err);
});
umami.track('generate', { copy: this.image });
}
},
},
mounted: function () {
this.updateImageLink();
}
});