-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbook.html
More file actions
342 lines (282 loc) · 9.7 KB
/
book.html
File metadata and controls
342 lines (282 loc) · 9.7 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link rel="manifest" href="manifest.json">
<title>Fb2 Reader</title>
<link rel="preconnect" href="https://fonts.googleapis.com">
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link href="https://fonts.googleapis.com/css2?family=Ubuntu:wght@400;700&display=swap" rel="stylesheet">
<style>
body {
font-family: system-ui, -apple-system, sans-serif;
background-color: #000;
color: #fff;
margin: 20px;
text-align: center;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
-webkit-touch-callout: none;
-webkit-tap-highlight-color: transparent;
}
.clickable-word {
transition: color 0.0s ease;
cursor: pointer;
display: inline-block;
}
h1 {
color: #fff;
}
#controls {
display: flex;
justify-content: space-between;
align-items: center;
margin: 0 0 20px 0;
}
#loadFileButton, #changeFontButton {
padding: 10px 20px;
font-family: 'Ubuntu', sans-serif;
font-size: 16px;
font-weight: bold;
border-radius: 0px;
cursor: pointer;
transition: background-color 0.3s, transform 0.2s;
border: none;
width: 120px;
text-align: center;
}
#loadFileButton {
background-color: #ffd700;
color: #000080;
}
#changeFontButton {
background-color: #000080;
color: #ffd700;
}
#loadFileButton:hover {
background-color: #ffa500;
}
#changeFontButton:hover {
background-color: #191970;
}
#loadFileButton:active, #changeFontButton:active {
transform: scale(0.95);
}
#fileInput {
display: none;
}
#leftMenuSelectMenu {
position: absolute;
top: 60px;
left: 20px;
background-color: #222;
border: 1px solid #ffd700;
padding: 5px;
z-index: 100;
display: flex;
flex-direction: column;
}
#leftMenuSelectMenu button {
background: none;
color: #ddd;
border: none;
font-size: 1.1em;
padding: 5px 10px;
width: 100%;
text-align: left;
cursor: pointer;
}
#leftMenuSelectMenu button:hover {
background-color: #ffd700;
color: #000;
}
#fontSelectMenu {
position: absolute;
top: 60px;
right: 20px;
background-color: #222;
border: 1px solid #ffd700;
padding: 5px;
z-index: 100;
display: flex;
flex-direction: column;
}
#fontSelectMenu button {
background: none;
color: #ddd;
border: none;
font-size: 1.1em;
padding: 5px 10px;
width: 100%;
text-align: right;
cursor: pointer;
}
#fontSelectMenu button:hover {
background-color: #ffd700;
color: #000;
}
.title {
font-size: 1.5em;
font-weight: bold;
margin: 10px 0;
}
.author {
font-size: 1em;
color: #ccc;
}
.section-title {
font-size: 1.2em;
margin-top: 20px;
color: #ffd700;
}
.paragraph {
margin: 10px 0;
line-height: 1.6;
color: #ddd;
}
</style>
</head>
<body>
<script src="pwa-init.js"></script>
<div id="controls">
<button id="loadFileButton">Menu</button>
<button id="changeFontButton">Font</button>
</div>
<input type="file" id="fileInput" accept=".fb2">
<div id="leftMenuSelectMenu" style="display: none;">
<button id="loadBookMenuItem">Load book</button>
<button id="helpMenuItem">Origin</button>
</div>
<div id="fontSelectMenu" style="display: none;">
<button data-font="Courier New">Courier</button>
<button data-font="Ubuntu">Ubuntu</button>
<button data-font="San Francisco">System</button>
</div>
<div id="content" style="margin-top: 20px;"></div>
<script>
const loadFileButton = document.getElementById('loadFileButton');
const fileInput = document.getElementById('fileInput');
const changeFontButton = document.getElementById('changeFontButton');
const fontSelectMenu = document.getElementById('fontSelectMenu');
const contentDiv = document.getElementById('content');
const leftMenuSelectMenu = document.getElementById('leftMenuSelectMenu');
const loadBookMenuItem = document.getElementById('loadBookMenuItem');
const helpMenuItem = document.getElementById('helpMenuItem');
function setMenuWidth() {
const leftButtonWidth = loadFileButton.offsetWidth;
leftMenuSelectMenu.style.width = leftButtonWidth + 'px';
const rightButtonWidth = changeFontButton.offsetWidth;
fontSelectMenu.style.width = rightButtonWidth + 'px';
}
document.addEventListener('DOMContentLoaded', setMenuWidth);
window.addEventListener('resize', setMenuWidth);
loadFileButton.addEventListener('click', (event) => {
event.stopPropagation();
leftMenuSelectMenu.style.display = leftMenuSelectMenu.style.display === 'block' ? 'none' : 'block';
fontSelectMenu.style.display = 'none';
});
changeFontButton.addEventListener('click', (event) => {
event.stopPropagation();
fontSelectMenu.style.display = fontSelectMenu.style.display === 'block' ? 'none' : 'block';
leftMenuSelectMenu.style.display = 'none';
});
loadBookMenuItem.addEventListener('click', () => {
fileInput.click();
leftMenuSelectMenu.style.display = 'none';
});
helpMenuItem.addEventListener('click', () => {
window.location.href = 'index.html';
leftMenuSelectMenu.style.display = 'none';
});
fileInput.addEventListener('change', function(event) {
const file = event.target.files[0];
if (!file) {
alert("Файл не выбран!");
return;
}
contentDiv.innerHTML = "";
if (file.name.toLowerCase().endsWith('.fb2')) {
const reader = new FileReader();
reader.onload = function(e) {
const parser = new DOMParser();
const xmlDoc = parser.parseFromString(e.target.result, "application/xml");
if (xmlDoc.getElementsByTagName("parsererror").length > 0) {
contentDiv.innerHTML = `<div style="color:red;">Ошибка: Файл FB2 поврежден или не является действительным XML.</div>`;
return;
}
const titleInfo = xmlDoc.querySelector("description > title-info");
if (titleInfo) {
const bookTitle = titleInfo.querySelector("book-title")?.textContent || "Без названия";
const authorFirstName = titleInfo.querySelector("author > first-name")?.textContent || "";
const authorLastName = titleInfo.querySelector("author > last-name")?.textContent || "Неизвестный автор";
const author = `${authorFirstName} ${authorLastName}`.trim();
contentDiv.innerHTML += `<div class="title">${bookTitle}</div>`;
contentDiv.innerHTML += `<div class="author">Автор: ${author}</div>`;
}
const body = xmlDoc.querySelector("body");
if (body) {
body.querySelectorAll("section").forEach(section => {
const sectionTitle = section.querySelector("title > p")?.textContent;
if (sectionTitle) {
contentDiv.innerHTML += `<div class="section-title">${sectionTitle}</div>`;
}
section.querySelectorAll("p").forEach(paragraph => {
const pDiv = document.createElement('div');
pDiv.className = 'paragraph';
pDiv.innerHTML = paragraph.textContent.split(/(\s+)/).map(part => {
if (part.trim().length === 0) return part;
return `<span class="clickable-word">${part}</span>`;
}).join('');
contentDiv.appendChild(pDiv);
});
});
} else {
contentDiv.innerHTML = "Не удалось найти содержимое книги.";
}
};
reader.readAsText(file);
} else {
alert("Неподдерживаемый тип файла. Выберите только FB2.");
}
});
fontSelectMenu.querySelectorAll('button').forEach(button => {
button.addEventListener('click', function() {
const selectedFont = this.getAttribute('data-font');
let fontCSS;
if (selectedFont === 'Ubuntu') {
fontCSS = "'Ubuntu', sans-serif";
} else if (selectedFont === 'San Francisco') {
fontCSS = 'system-ui, -apple-system, sans-serif';
} else {
fontCSS = "'Courier New', monospace";
}
document.body.style.fontFamily = fontCSS;
fontSelectMenu.style.display = 'none';
});
});
document.addEventListener('click', function(event) {
if (!leftMenuSelectMenu.contains(event.target) && !loadFileButton.contains(event.target)) {
leftMenuSelectMenu.style.display = 'none';
}
if (!fontSelectMenu.contains(event.target) && !changeFontButton.contains(event.target)) {
fontSelectMenu.style.display = 'none';
}
});
document.addEventListener('click', function(e) {
if (e.target.classList.contains('clickable-word')) {
e.preventDefault();
const targetGreen = '#00ff00';
const currentStyle = window.getComputedStyle(e.target).color;
if (e.target.style.color === 'rgb(0, 255, 0)' || e.target.style.color === targetGreen) {
e.target.style.color = '';
} else {
e.target.style.color = targetGreen;
}
}
}, true);
</script>
</body>
</html>