-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
252 lines (237 loc) · 11.6 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="description" content="Lithium Ebook Highlighted Text Extractor" />
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
<title>Lithium Text Extractor</title>
</head>
<body>
<div class="container mt-4">
<div class="row">
<div class="col-sm">
<h1>Lithium Ebook Highlighted Text Extractor</h1>
</div>
</div>
<div class="row">
<div class="mb-3">
<label for="input-file" class="form-label">Select file (Exported from Lithium)</label>
<input id="input-file" class="form-control" type="file" />
</div>
</div>
<div class="row">
<div class="col-sm">
<div class="mb-3">
<label for="input-text" class="form-label">Content here (HTML Code)</label>
<textarea id="input-text" style="min-height: 100%;" class="form-control min-vh-40" id="exampleFormControlTextarea1" rows="4" spellcheck="false"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-sm">
<div class="mb-3">
<div class="btn-group" role="group" aria-label="Basic outlined example">
<select id="color-dropdown" class="form-select" aria-label="Select color" onchange="changeDropdownColor()">
<option selected>All colors</option>
</select>
</div>
<button type="button" class="btn btn-outline-primary btn-block" onclick="process()">Process</button>
<button type="button" class="btn btn-outline-success btn-block" onclick="copy()">Copy to clipboard</button>
</div>
</div>
</div>
<div class="row">
<div class="col-sm">
<div class="mb-3">
<label for="output-text" class="form-label">Output (Extracted text here)</label>
<textarea id="output-text" style="height: 80%;" class="form-control" id="exampleFormControlTextarea1" rows="4" spellcheck="false"></textarea>
</div>
</div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/js/bootstrap.bundle.min.js" integrity="sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM" crossorigin="anonymous"></script>
<script>
window.onload = function () {
document.getElementById("input-text").value = " ";
document.getElementById("output-text").value = " ";
document.getElementById("input-file").value = null;
clearColorDropdownItems();
addColorDropdownItem(false, "All colors");
};
function process() {
document.getElementById("output-text").value = " ";
let input = document.getElementById("input-text").value;
if (input.trim() == "") {
alert("Select a file or paste its content");
return;
}
if (!input.includes("<html")) {
alert("Wrong input");
return;
}
let output = input;
let colorSelected = document.getElementById("color-dropdown").value;
let listRegexNoColor = [
[String.raw`^(?!<div class='highlight'>).+`, ``],
[String.raw`<div class='highlight'>`, ``],
[String.raw`</div>`, ``],
];
let listRegex1Color = [
[String.raw`^(?!(<div class='highlight'>)|(<div class='color')).+`, ``],
[String.raw`^(?:[\t ]*(?:\r?\n|\r))+`, ``],
[String.raw`(<div class='color' style='background-color: ${colorSelected}'></div>)([\s\S])(<div class='highlight'>)(.*)(</div>)`, "$4"],
[String.raw`.*<div class='color'.*`, ``],
[String.raw`.*<div class='highlight'>.*`, ``],
[String.raw`^(?:[\t ]*(?:\r?\n|\r))+`, ``],
];
let listRegexFix = [
[String.raw`^(?:[\t ]*(?:\r?\n|\r))+`, ``],
[String.raw` `, ``],
[String.raw` `, ` `],
[String.raw` `, ` `],
[String.raw`’`, `'`],
[String.raw`é`, `è`],
[String.raw`à`, `à`],
[String.raw`ò`, `ò`],
[String.raw`ù`, `ù`],
[String.raw`è`, `è`],
[String.raw`È`, `è`],
[String.raw`«`, ``],
[String.raw`»`, ``],
[String.raw`–`, ``],
[String.raw`ì`, `i`],
[String.raw`“`, ``],
[String.raw`”`, ``],
[String.raw`—`, ``],
[String.raw` `, ``],
[String.raw` `, ` `],
[String.raw` `, ` `],
[String.raw`(^ )|( $)`, ` `],
];
if (colorSelected.includes("#")) {
listRegex1Color.forEach(function (regexText) {
output = output.replace(new RegExp(regexText[0], "gm"), regexText[1]);
});
} else {
listRegexNoColor.forEach(function (regexText) {
output = output.replace(new RegExp(regexText[0], "gm"), regexText[1]);
});
}
listRegexFix.forEach(function (regexText) {
output = output.replace(new RegExp(regexText[0], "gm"), regexText[1]);
});
document.getElementById("output-text").value = output;
if (isDesktop) {
selectField("output-text");
}
}
document.getElementById("input-file").addEventListener("change", getFile);
function getFile(event) {
const input = event.target;
if ("files" in input && input.files.length > 0) {
document.getElementById("input-text").value = " ";
document.getElementById("output-text").value = " ";
placeFileContent(document.getElementById("input-text"), input.files[0]);
setTimeout(function () {
if (isDesktop) {
selectField("input-text");
}
setColorsFromInput();
}, 500);
}
}
function placeFileContent(target, file) {
readFileContent(file)
.then((content) => {
target.value = content;
})
.catch((error) => console.log(error));
}
function readFileContent(file) {
const reader = new FileReader();
return new Promise((resolve, reject) => {
reader.onload = (event) => resolve(event.target.result);
reader.onerror = (error) => reject(error);
reader.readAsText(file);
});
}
function copy() {
selectField("output-text");
document.execCommand("copy");
alert("Output copied to clipboard.");
}
function selectField(fieldId) {
let field = document.getElementById(fieldId);
field.select();
field.setSelectionRange(0, 999999);
}
function isDesktop() {
if (typeof window.orientation == "undefined") {
return True;
} else {
return False;
}
}
function addColorDropdownItem(changeColor, colorItem) {
let selectColor = document.getElementById("color-dropdown");
let optionColor = document.createElement("option");
if (changeColor) {
optionColor.setAttribute("style", "background-color: " + colorItem + " ");
} else {
optionColor.setAttribute("style", "background-color: #ffffff");
}
let text = document.createTextNode(colorItem);
optionColor.appendChild(text);
selectColor.appendChild(optionColor);
}
function clearColorDropdownItems() {
let selectColor = document.getElementById("color-dropdown");
while (selectColor.firstChild) {
selectColor.removeChild(selectColor.firstChild);
}
}
function setColorsFromInput() {
clearColorDropdownItems();
let colors = [];
let input = document.getElementById("input-text").value;
input = input.replace(new RegExp(String.raw`^(?!<div class='color' style='background-color).+`, "mg"), ``);
input = input.replaceAll(String.raw`<div class='color' style='background-color: `, ``);
input = input.replaceAll(String.raw`'></div>`, ``);
input = input.replace(new RegExp(String.raw`^(?:[\t ]*(?:\r?\n|\r))+`, "mg"), ``);
let char = "\n";
let i = (j = 0);
while ((j = input.indexOf(char, i)) !== -1) {
colors.push(input.substring(i, j).trim());
i = j + 1;
}
let colorsUniq = remove_duplicates(colors);
addColorDropdownItem(false, "All colors");
for (let i = 0; i < colorsUniq.length; i++) {
addColorDropdownItem(true, colorsUniq[i]);
}
changeDropdownColor();
}
function changeDropdownColor() {
let selectColor = document.getElementById("color-dropdown");
if (selectColor.value.includes("#")) {
selectColor.setAttribute("style", "background-color: " + selectColor.value + " ");
} else {
selectColor.setAttribute("style", "background-color: #ffffff");
}
}
function remove_duplicates(arr) {
let obj = {};
let ret_arr = [];
for (let i = 0; i < arr.length; i++) {
obj[arr[i]] = true;
}
for (let key in obj) {
ret_arr.push(key);
}
return ret_arr;
}
</script>
</body>
</html>