-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtranslate.html
More file actions
393 lines (329 loc) · 15 KB
/
Copy pathtranslate.html
File metadata and controls
393 lines (329 loc) · 15 KB
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
<!DOCTYPE html>
<!-- Based on https://sayonari.coresv.com/ninshikiChan/text.html -->
<html>
<head>
<meta charset="UTF-8">
<link href="https://fonts.googleapis.com/css?family=M+PLUS+Rounded+1c&display=swap" rel="stylesheet">
<title>Live Translation</title>
<style type="text/css">
/* cousine-regular - latin */
@font-face {
font-family: 'Cousine';
font-style: normal;
font-weight: 400;
src: url('./fonts/cousine-v17-latin-regular.eot');
/* IE9 Compat Modes */
src: local(''),
url('./fonts/cousine-v17-latin-regular.eot?#iefix') format('embedded-opentype'),
/* IE6-IE8 */
url('./fonts/cousine-v17-latin-regular.woff2') format('woff2'),
/* Super Modern Browsers */
url('./fonts/cousine-v17-latin-regular.woff') format('woff'),
/* Modern Browsers */
url('./fonts/cousine-v17-latin-regular.ttf') format('truetype'),
/* Safari, Android, iOS */
url('./fonts/cousine-v17-latin-regular.svg#Cousine') format('svg');
/* Legacy iOS */
}
button,
input,
select,
textarea {
/* font-family : inherit; */
/* font-size : 300%; */
/* color : black; */
font-weight: 0;
text-align: center;
/* left, center, right */
vertical-align: top;
/* top, middle, bottom */
-webkit-text-stroke-color: rgb(21, 0, 141);
-webkit-text-stroke-width: 0px;
}
html {
height: 100%;
width: 100%;
}
body {
height: 100%;
width: 100%;
margin: 0;
font-family: 'Cousine', sans-serif;
}
table {
width: 100%;
/* table-layout: fixed; */
margin-bottom: 50px;
}
table.btm_table {
position: absolute;
/* bottom:0; */
}
table td {
/*word-break: break-all;*/
overflow-wrap: break-word;
}
</style>
<style>
/* prepare the selectors to add a stroke to */
.stroke-single-imb {
/* position: absolute; */
left: 0;
right: 0;
margin: 0;
/* -webkit-text-stroke: 0px #0000FF; */
}
.stroke-single-bg {
position: absolute;
left: 0;
right: 0;
margin: auto;
/* -webkit-text-stroke: 2px #FF0000; */
}
/* add a single stroke */
.stroke-single-fg {
position: absolute;
left: 0;
right: 0;
margin: auto;
/* -webkit-text-stroke: 0px #FFFFFF; */
}
</style>
<script>
var flag_speech = 0;
// Speech recognition starts when window is loaded --------------
window.onload = function () {
vr_function();
}
// Function for getting URL parameters ---------------------------
function getParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
function vr_function() {
window.SpeechRecognition = window.SpeechRecognition || webkitSpeechRecognition;
// Settings for voice recognition ----------------------
var recognition = new webkitSpeechRecognition();
recognition.lang = 'fr';
recognition.interimResults = true;
recognition.continuous = true;
var recog_text = '';
var trans_text = '';
// Translation settings ---------------------------
var trans_sourcelang = 'fr';
var trans_destlang = 'en-US';
var gs_key = getParam('gs_key');
var TRANS_URL = 'https://script.google.com/macros/s/' + gs_key + '/exec';
TRANS_URL = 'https://api-free.deepl.com/v2/translate'
var DEEPL_KEY = '83e31615-d5fa-1097-d2aa-d7e99a8559b7:fx'
var query = ''
///////////////////////////////////////////////////////////
// Events ---------------------------------
recognition.onsoundstart = function () {
// document.getElementById('status').innerHTML = "Listening";
};
recognition.onspeechstart = function () {
// document.getElementById('status').innerHTML = "Listening";
};
recognition.onnomatch = function () {
// document.getElementById('status').innerHTML = "Please try again";
};
recognition.onerror = function () {
// document.getElementById('status').innerHTML = "Error";
vr_function();
};
recognition.onsoundend = function () {
// document.getElementById('status').innerHTML = "Stopping";
recognition.stop()
vr_function();
};
recognition.onspeechend = function () {
// document.getElementById('status').innerHTML = "Stopping";
recognition.stop()
vr_function();
};
//////////////////////////////////////////////////////////
// URL parameters -------------------
arg_recog = getParam('recog');
arg_trans = getParam('trans');
// Language setting ----------------------------
if (arg_recog != null) {
recognition.lang = arg_recog;
trans_sourcelang = recognition.lang;
}
if (arg_trans != null) {
trans_destlang = arg_trans;
}
if (trans_sourcelang == trans_destlang) {
alert("ERROR! Please set different language between recog and trans!\nYou set both [" + trans_sourcelang + "]!");
}
/////////////////////////////////////////////////////////
// API settings ---------------------------
var request = new XMLHttpRequest();
// When the recognition result is returned ------------------
recognition.onresult = function (event) {
var results = event.results;
for (var i = event.resultIndex; i < results.length; i++) {
if (results[i].isFinal) {
// console.log(results);
recog_text = results[i][0].transcript;
// document.getElementById('speech_text-imb').innerHTML = recog_text;
// document.getElementById('speech_text-bg').innerHTML = recog_text;
// document.getElementById('speech_text-fg').innerHTML = recog_text;
query = TRANS_URL //+ `&text=${recog_text}&source_lang=${trans_sourcelang}&target_lang=${trans_destlang}`;
fetch(query, {
method: 'POST',
headers: {
'Access-Control-Allow-Credentials': 'true',
'Access-Control-Allow-Headers': 'access-control-allow,access-control-allow-credentials,access-control-allow-headers,access-control-allow-methods,authorization,control-allow-headers,cross-origin-resource-policy,deepl-auth-key,wildcard',
'Access-Control-Allow-Methods': 'DELETE, POST, GET, OPTIONS',
'Access-Control-Allow-Origin': 'https://enthusiastic-beds-production.up.railway.app',
'Access-Control-Expose-Headers': 'Server-Timing',
'Authorization': "DeepL-Auth-Key " + DEEPL_KEY,
'DeepL-Auth-Key': DEEPL_KEY,
'wildcard': '*',
'Cross-Origin-Resource-Policy': 'cross-origin',
'Access-Control-Allow': '*',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36',
'content-length': query.length,
'Content-Type': 'application/json',
'Referrer-Policy': 'no-referrer',
},
body: JSON.stringify({
'text': [recog_text],
'source_lang': trans_sourcelang,
'target_lang': trans_destlang,
}),
referrerPolicy: 'no-referrer',
}).then(response => { console.log(response); return response.json(); }).then(data => {
console.log(data)
trans_text = data.translations[0].text;
console.log(trans_text)
document.getElementById('trans_text-imb').innerHTML = trans_text;
document.getElementById('trans_text-bg').innerHTML = trans_text;
document.getElementById('trans_text-fg').innerHTML = trans_text;
//if (data.readyState === 4 && data.status === 200) {
// document.getElementById('speech_text-imb').innerHTML = recog_text;
// document.getElementById('speech_text-bg').innerHTML = recog_text;
// document.getElementById('speech_text-fg').innerHTML = recog_text;
// response = JSON.parse(request.responseText);
//}
vr_function()
});
if (gs_key != null) {
} else {
document.getElementById('speech_text-imb').innerHTML = recog_text;
document.getElementById('speech_text-bg').innerHTML = recog_text;
document.getElementById('speech_text-fg').innerHTML = recog_text;
vr_function();
}
}
else {
document.getElementById('speech_text-imb').innerHTML = "<< " + results[i][0].transcript + " >>";
document.getElementById('speech_text-bg').innerHTML = "<< " + results[i][0].transcript + " >>";
document.getElementById('speech_text-fg').innerHTML = "<< " + results[i][0].transcript + " >>";
flag_speech = 1;
}
}
}
flag_speech = 0;
recognition.start();
}
</script>
</head>
<body>
<div class="big" id="result_text">
<table id="text_table" class="btm_table">
<tr>
<td id="tbl_td" align="center" valign='bottom'>
<div class="stroke-single-bg" id="speech_text-bg">
[ TRANSLATING HERE ]
</div>
<div class="stroke-single-fg" id="speech_text-fg">
[ TRANSLATING HERE ]
</div>
<div class="stroke-single-imb" id="speech_text-imb">
[ TRANSLATING HERE ]
</div>
<div class="stroke-single-bg" id="trans_text-bg">
</div>
<div class="stroke-single-fg" id="trans_text-fg">
</div>
<div class="stroke-single-imb" id="trans_text-imb">
</div>
</td>
</tr>
</table>
</div>
</body>
<!-- ############## Last script ############## -->
<script type="text/javascript">
// Change display style ---------------------------------
if (getParam('bgcolor') != null) {
document.bgColor = getParam('bgcolor');
}
if (getParam('v_align') == "top") {
document.getElementById("text_table").style.bottom = -1;
} else if (getParam('v_align') == "bottom") {
document.getElementById("text_table").style.bottom = 0;
}
// Adjust the background color
if (getParam('bgcolor') != null) {
document.getElementById("speech_text-imb").style.webkitTextStrokeColor = getParam('bgcolor');
}
if (getParam('st_width') != null) {
document.getElementById("speech_text-imb").style.webkitTextStrokeWidth = getParam('st_width') + 'pt';
}
if (getParam('bgcolor') != null) {
document.getElementById("trans_text-imb").style.webkitTextStrokeColor = getParam('bgcolor');
}
if (getParam('st_width') != null) {
document.getElementById("trans_text-imb").style.webkitTextStrokeWidth = getParam('st_width')*2 + 'pt';
}
// Speech recognition result text
if (getParam('font') != null) {
document.getElementById("result_text").style.fontFamily = "'" + getParam('font') + "'";
}
if (getParam('size') != null) {
document.getElementById("result_text").style.fontSize = getParam('size') + 'pt';
}
if (getParam('color') != null) {
document.getElementById("speech_text-fg").style.color = "gray";
}
if (getParam('st_color') != null) {
document.getElementById("speech_text-bg").style.webkitTextStrokeColor = getParam('st_color');
}
if (getParam('weight') != null) {
document.getElementById("result_text").style.fontWeight = getParam('weight');
}
if (getParam('st_width') != null) {
document.getElementById("speech_text-bg").style.webkitTextStrokeWidth = getParam('st_width') + 'pt';
}
// Translation result text
document.getElementById("trans_text-fg").style.fontSize = "34pt";
if (getParam('color') != null) {
document.getElementById("trans_text-fg").style.color = getParam('color');
}
if (getParam('st_color') != null) {
document.getElementById("trans_text-bg").style.webkitTextStrokeColor = getParam('st_color');
}
document.getElementById("trans_text-bg").style.fontSize = "34pt";
if (getParam('st_width') != null) {
document.getElementById("trans_text-bg").style.webkitTextStrokeWidth = getParam('st_width')*1.3 + 'pt';
}
function getParam(name, url) {
if (!url) url = window.location.href;
name = name.replace(/[\[\]]/g, "\\$&");
var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
results = regex.exec(url);
if (!results) return null;
if (!results[2]) return '';
return decodeURIComponent(results[2].replace(/\+/g, " "));
}
</script>
</html>