-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
168 lines (160 loc) · 5.54 KB
/
index.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
var count = 0
var count_2 = 0
var interval = null
var interval_2 = null
document.getElementById('evaluate').addEventListener('click', async (event) => {
event.preventDefault();
const original = document.getElementById('originalLanguage');
const originalLanguage = original.options[original.selectedIndex].text;
const translated = document.getElementById('translatedLanguage');
const translatedLanguage = translated.options[translated.selectedIndex].text;
const originalText = document.getElementById('originalText').value
const translatedText = document.getElementById('translatedText').value
if (originalText != '' && translatedText != '' && originalLanguage != translatedLanguage) {
try {
const response = await fetch('api/translate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ originalText, translatedText, originalLanguage, translatedLanguage }),
});
const info = await response.json();
const text = info.response
.replace(/\n/g, '<br>')
.replace(/\*\*(.*?)\*\*/g, '<strong>$1</strong>')
.replace(/"([^"]*)"/g, '<span class="italic">"$1"</span>')
document.getElementById('info').innerHTML = `${text}`
} catch (error) {
console.error('Error translating text:', error);
}
}
if (originalText == '') {
const shakeAnim = [
{ transform: "translateX(0)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(5px)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(0)" },
];
const shakeTiming = {
duration: 250,
iterations: 2,
};
document.getElementById('originalText').animate(shakeAnim,shakeTiming)
}
if (translatedText == '') {
const shakeAnim = [
{ transform: "translateX(0)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(5px)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(0)" },
];
const shakeTiming = {
duration: 250,
iterations: 2,
};
document.getElementById('translatedText').animate(shakeAnim,shakeTiming)
}
document.getElementById('warning').innerHTML = ''
if (translatedText == '' || originalText == '') {
clearInterval(interval)
let warning;
if (translatedText == '' && originalText == '') {
warning = 'Please enter the original and tranlation text content'
}
else if (originalText == '') {
warning = 'Please enter the original text content'
}
else if (translatedText == '') {
warning = 'Please enter the tranlation text content'
}
document.getElementById('warning').innerHTML = ''
count = 0
interval = setInterval(WriteWarming, 10, warning, 'warning')
}
if (originalLanguage == translatedLanguage) {
clearInterval(interval)
const warning = 'The original language and the translation language cannot be the same'
document.getElementById('warning').innerHTML = ''
count = 0
interval = setInterval(WriteWarming, 10, warning, 'warning')
const shakeAnim = [
{ transform: "translateX(0)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(5px)" },
{ transform: "translateX(-5px)" },
{ transform: "translateX(0)" },
];
const shakeTiming = {
duration: 250,
iterations: 2,
};
document.getElementById('originalLanguage').animate(shakeAnim,shakeTiming)
document.getElementById('translatedLanguage').animate(shakeAnim,shakeTiming)
}
})
document.getElementById('generate').addEventListener('click', async (event) => {
event.preventDefault();
const value = document.getElementById('proficiencyRange').value
document.getElementById('translatedText').value = ''
let level = 'A1'
if (value == '0') {
level = 'A1'
}
else if (value == '1') {
level = 'A2'
}
else if (value == '2') {
level = 'B1'
}
else if (value == '3') {
level = 'B2'
}
else if (value == '4') {
level = 'C1'
}
else if (value == '5') {
level = 'C2'
}
console.log(level, value)
const original = document.getElementById('originalLanguage');
const originalLanguage = original.options[original.selectedIndex].text;
const topic = topics[Math.floor(Math.random() * topics.length)];
try {
const response = await fetch('api/random', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ level, topic, originalLanguage }),
});
const info = await response.json();
console.log(info);
const text = info.response
document.getElementById('originalText').value = ''
count_2 = 0
interval_2 = setInterval(WriteOriginalText, 10, text, 'originalText')
} catch (error) {
console.error('Error fetching random sentence:', error);
}
})
function WriteWarming(text, ID){
document.getElementById(`${ID}`).innerHTML += text[count]
count++
if (count >= text.length) {
clearInterval(interval)
count = 0
}
//console.log(count)
}
function WriteOriginalText(text, ID){
document.getElementById(`${ID}`).value += text[count_2]
count_2++
if (count_2 >= text.length) {
clearInterval(interval_2)
count_2 = 0
}
//console.log(count)
}