-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage-translation.html
217 lines (191 loc) · 6.1 KB
/
image-translation.html
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
<!DOCTYPE html>
<html>
<meta charset="utf8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<head>
<style>
body, html {
font-family: system-ui;
}
table {
border-collapse: collapse;
}
th, td {
padding: 0.5em;
border: 1px solid #DDD;
}
th {
background-color: #333;
color: white;
}
td.image {
text-align: center;
width: 100px;
}
td.image > a > img {
display: block;
width: 100px;
height: 100px;
margin: 1em 0;
overflow-x: scroll;
object-fit: contain;
border: 1px solid lightgray;
}
td.image > a > img:hover {
background-color: black;
}
td.text-recognition {
width: calc(48vw - 50px);
overflow-y: scroll;
white-space: pre-wrap;
font-family: monospace;
padding: 2em;
}
td.translation {
width: calc(48vw - 50px);
overflow-y: scroll;
white-space: pre-wrap;
font-family: monospace;
padding: 2em;
}
tr:nth-child(even) {
background-color: #EFEFEF;
}
input[type=button] {
font-size: 16px;
}
div + div {
margin-top: 1em;
}
.language-pair {
padding: 1em;
text-align: center;
font-size: 18px;
font-weight: bold;
}
tr.language-pair-row {
background-color: #555;
color: white;
}
</style>
<script>
function addRow(imageSource)
{
let template = document.querySelector("template.image-translation");
let row = template.content.cloneNode(true).children[0];
row.querySelector("td.image > a > img").src = imageSource;
row.querySelector("td.image > a").href = imageSource;
document.querySelector("tbody").appendChild(row);
return row;
}
function addDivider(sourceLID, targetLID)
{
let row = document.createElement("tr");
row.classList.add("language-pair-row");
let rowData = document.createElement("td");
rowData.colSpan = 3;
rowData.classList.add("language-pair");
let source = document.createElement("code");
source.textContent = sourceLID;
let target = document.createElement("code");
target.textContent = targetLID;
rowData.appendChild(document.createTextNode("Translating from "));
rowData.appendChild(source);
rowData.appendChild(document.createTextNode(" to "));
rowData.appendChild(target);
row.appendChild(rowData);
document.querySelector("tbody").appendChild(row);
return row;
}
let table;
let rowsByIdentifier = {};
function updateTable(line)
{
let translationMatch = line.match(/Translating page from '(.*)' to '(.*)'$/);
if (translationMatch) {
console.log("Adding divider.")
addDivider(translationMatch[1], translationMatch[2]);
return;
}
let startedMatch = line.match(/\[#(\d+)\] Image translation started for (.*)$/);
if (startedMatch) {
// line = `[#1] Image translation started for https://okky.kr/assets/images/okjsp_logo.png`
let identifier = parseInt(startedMatch[1]);
rowsByIdentifier[identifier] = addRow(startedMatch[2]);
return;
}
let noTextMatch = line.match(/\[#(\d+)\] Image translation completed in (\d+\.\d+) sec\. \(no text\)$/);
if (noTextMatch) {
// line = `[#1] Image translation completed in 0.287 sec. (no text)`
let identifier = parseInt(noTextMatch[1]);
let row = rowsByIdentifier[identifier];
if (!row)
return;
row.querySelector("td.text-recognition").textContent = `No text\n\n(${noTextMatch[2]} sec.)`;
return;
}
let recognizedTextMatch = line.match(/\[#(\d+)\] Image translation recognized text in (\d+\.\d+) sec\. \(line count: (\d+)\): /);
if (recognizedTextMatch) {
// line = `[#2] Image translation recognized text in 0.502 sec. (line count: 2): "2022 EA Korea Summer Internship\n|지원기간 | 2022.05.02(월) - 05.22(일) 23:59까지"`
let identifier = parseInt(recognizedTextMatch[1]);
let row = rowsByIdentifier[identifier];
if (!row)
return;
let stringToShow = line.substring(line.indexOf(recognizedTextMatch[0]) + recognizedTextMatch[0].length);
stringToShow = stringToShow.replaceAll("\\n", "\n");
row.querySelector("td.text-recognition").textContent = `${stringToShow}\n\n(${recognizedTextMatch[2]} sec.)`;
return;
}
let translatedTextMatch = line.match(/\[#(\d+)\] Image translation completed in (\d+\.\d+) sec\. \(paragraph count: (\d+)\): /);
if (translatedTextMatch) {
// line = `[#2] Image translation completed in 0.991 sec. (paragraph count: 2): "2022 EA Korea Summer Internship\n|Application period | 2022.05.02 (Mon) - 05.22 (Sun) 23:59"`
let identifier = parseInt(translatedTextMatch[1]);
let row = rowsByIdentifier[identifier];
if (!row)
return;
let stringToShow = line.substring(line.indexOf(translatedTextMatch[0]) + translatedTextMatch[0].length);
stringToShow = stringToShow.replaceAll("\\n", "\n");
row.querySelector("td.translation").textContent = `${stringToShow}\n\n(${translatedTextMatch[2]} sec.)`;
parseFloat(translatedTextMatch[2]);
return;
}
}
function consume(logOutput)
{
logOutput.split("\n").map(updateTable);
}
function handleDropOrPaste(event)
{
event.preventDefault();
const dataTransfer = event.dataTransfer ? event.dataTransfer : event.clipboardData;
for (let line of dataTransfer.getData("text/plain").split("\n"))
consume(line);
document.querySelector("table").style.display = "";
document.designMode = "off";
}
addEventListener("load", () => {
document.body.addEventListener("drop", handleDropOrPaste);
document.body.addEventListener("paste", handleDropOrPaste);
document.designMode = "on";
});
</script>
</head>
<body>
<table style="display: none;">
<tbody>
<tr id="table-header-container">
<th>Image</th>
<th>Text Recognition</th>
<th>Translation</th>
</tr>
</tbody>
</table>
<template class="image-translation">
<tr>
<td class="image"><a target="_blank"><img></a></td>
<td class="text-recognition"></td>
<td class="translation"></td>
</tr>
</template>
</body>
</html>