-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
280 lines (245 loc) · 8.49 KB
/
Copy pathindex.html
File metadata and controls
280 lines (245 loc) · 8.49 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
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<title>Html-only Cbz Reader</title>
<style>
html, body {
overflow-x: hidden;
margin: 0;
padding: 0;
height: 100vh;
color: white;
background-color: rgb(0,0,0);
font-family: Verdana, sans-serif;
display: flex;
flex-direction: column;
}
#bar {
display: flex;
align-items: center;
gap: 10px;
position: sticky;
background: rgb(11,11,11);
height: 35px;
padding: 5px 10px;
}
#bar input {
background: rgb(22,22,22);
padding: 5px;
border: none;
width: 500px;
}
#bar input[type="range"] {
accent-color: rgb(22,22,22);
width: 200px;
}
#bar input[type="number"] {
color: white;
width: 50px;
margin-left: 10px;
}
#imageContainer {
flex-grow: 1;
text-align: center;
overflow: auto;
}
img {
max-width: 100%;
max-height: 100vh;
display: block;
margin: auto;
padding-bottom: 5px;
}
#pageCounter {
margin-left: auto;
white-space: nowrap;
}
#messageContainer {
width: 100%;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
text-align: center;
pointer-events: none;
}
.message {
font-size: 32px;
text-shadow: 0 0 4px #000;
pointer-events: none;
}
#loadingMessage {
opacity: 0%;
transition: opacity 0.5s ease-in-out;
}
</style>
</head>
<body>
<div id="messageContainer">
<p class="message" id="loadingMessage">Loading... (0 / 0)</p>
</div>
<div id="imageContainer"></div>
<div id="bar">
<input type="file" id="fileInput" accept=".cbz">
<text>Zoom:</text>
<input type="range" id="zoomSlider" min="60" max="160" value="100">
<input type="number" id="zoomInput" min="60" max="160" value="100" step="1">
<text id="pageCounter">Page 0 / 0</text>
</div>
</body>
<script>
const imageContainer = document.getElementById('imageContainer');
const loadingMessage = document.getElementById("loadingMessage");
const pageCounter = document.getElementById('pageCounter');
const zoomSlider = document.getElementById("zoomSlider");
const zoomInput = document.getElementById("zoomInput");
let imageOffsets = [];
document.getElementById('fileInput').addEventListener('change', async function(event) {
loadingMessage.textContent = `Loading... (0 / 0)`;
const file = event.target.files[0];
if (!file) return; // verify that at least one file exists
loadingMessage.style.opacity = "1";
const totalPages = await loadBook(file);
if (totalPages == 0) {
console.log(`'${file.name}' failed to load. Please check encoding...`);
setTimeout(() => {
loadingMessage.style.opacity = "0";
}, 500);
}
else {
console.log(`'${file.name}' loaded with ${totalPages} pages. Happy reading! =)`);
setTimeout(() => {
loadingMessage.style.opacity = "0";
}, 500);
// Set up zooming after images are loaded
setupZoom(totalPages);
// Page counter logic
imageContainer.addEventListener('scroll', function() {
setPageCounter(totalPages);
});
}
});
async function loadBook(file) {
const d = new Uint8Array(await file.arrayBuffer()), len = d.length;
let e = len - 22;
while (e >= 0 && !(d[e]===0x50 && d[e+1]===0x4b && d[e+2]===0x05 && d[e+3]===0x06)) e--;
if (e < 0) { alert("Could not load cbz file. It may be 7z encoded. Please use zip encoded cbz files only!"); return 0; }
const num = d[e+10] | (d[e+11]<<8);
let off = d[e+16] | (d[e+17]<<8) | (d[e+18]<<16) | (d[e+19]<<24), entries = [];
for (let i = 0; i < num; i++) {
const comp = d[off+10] | (d[off+11]<<8), csize = d[off+20] | (d[off+21]<<8) | (d[off+22]<<16) | (d[off+23]<<24), usize = d[off+24] | (d[off+25]<<8) | (d[off+26]<<16) | (d[off+27]<<24), nlen = d[off+28] | (d[off+29]<<8), elen = d[off+30] | (d[off+31]<<8), clen = d[off+32] | (d[off+33]<<8), loff = d[off+42] | (d[off+43]<<8) | (d[off+44]<<16) | (d[off+45]<<24);
let name = '';
for (let j = 0; j < nlen; j++) name += String.fromCharCode(d[off+46+j]);
off += 46 + nlen + elen + clen;
entries.push({ name, comp, csize, usize, loff });
}
const imageFiles = entries.filter(e => e.name.match(/\.(jpg|jpeg|png|gif)$/i)).sort((a, b) => a.name < b.name ? -1 : 1);
imageContainer.innerHTML = '';
const totalPages = imageFiles.length;
let pagesLoaded = 1;
const loadPromises = [];
for (const entry of imageFiles) {
const fnlen = d[entry.loff+26] | (d[entry.loff+27]<<8), exlen = d[entry.loff+28] | (d[entry.loff+29]<<8), dataOff = entry.loff + 30 + fnlen + exlen;
const compressed = d.subarray(dataOff, dataOff + entry.csize);
let imgData;
if (entry.comp === 0) {
imgData = new Blob([compressed]);
} else if (entry.comp === 8) {
const ds = new DecompressionStream('deflate-raw'), writer = ds.writable.getWriter();
await writer.write(compressed); await writer.close();
const reader = ds.readable.getReader(), chunks = [];
while (true) { const {value, done} = await reader.read(); if (done) break; chunks.push(value); }
const result = new Uint8Array(entry.usize); let pos = 0;
for (const chunk of chunks) { result.set(chunk, pos); pos += chunk.length; }
imgData = new Blob([result]);
} else continue;
const imgUrl = URL.createObjectURL(imgData);
const img = document.createElement('img');
img.src = imgUrl;
imageContainer.appendChild(img);
loadPromises.push(new Promise(resolve => { img.onload = resolve; img.onerror = resolve; }));
loadingMessage.textContent = `Loading... (${pagesLoaded} / ${totalPages})`;
pagesLoaded += 1;
}
await Promise.all(loadPromises);
computeImageOffsets();
return totalPages;
}
function computeImageOffsets() {
const images = document.querySelectorAll("#imageContainer img");
imageOffsets = [];
let cumulative = 0;
images.forEach(img => {
cumulative += (img.offsetHeight || 0) + 5;
imageOffsets.push(cumulative);
});
}
function setPageCounter(pageCount) {
const scrollPosition = imageContainer.scrollTop;
let currentPage = 1;
for (let i = 0; i < imageOffsets.length; i++) {
if (scrollPosition < imageOffsets[i]) {
currentPage = i + 1;
break;
}
}
if (imageOffsets.length > 0 && scrollPosition >= imageOffsets[imageOffsets.length - 1]) {
currentPage = pageCount;
}
pageCounter.textContent = `Page ${currentPage} / ${pageCount}`;
}
// Function to update zoom
function updateZoom(value, pageCount) {
// Find which page is currently at the top of the viewport
const scrollTop = imageContainer.scrollTop;
let targetPage = 0;
for (let i = 0; i < imageOffsets.length; i++) {
if (scrollTop < imageOffsets[i]) {
targetPage = i;
break;
}
}
if (imageOffsets.length > 0 && scrollTop >= imageOffsets[imageOffsets.length - 1]) {
targetPage = imageOffsets.length - 1;
}
// How far into that page we've scrolled
const pageStart = targetPage > 0 ? imageOffsets[targetPage - 1] - 5 : 0;
const offsetInPage = scrollTop - pageStart;
const images = document.querySelectorAll("#imageContainer img");
images.forEach(img => {
img.style.maxHeight = `${value}vh`;
});
computeImageOffsets();
// Restore scroll to keep the same page in view
const newPageStart = targetPage > 0 ? imageOffsets[targetPage - 1] - 5 : 0;
const imageHeight = imageOffsets[targetPage] - (targetPage > 0 ? imageOffsets[targetPage - 1] - 5 : 0) - 5;
const fraction = imageHeight > 0 ? Math.min(offsetInPage, imageHeight) : 0;
imageContainer.scrollTop = Math.min(newPageStart + fraction, imageContainer.scrollHeight - imageContainer.clientHeight);
setPageCounter(pageCount);
}
function setupZoom(pageCount) {
const images = document.querySelectorAll("#imageContainer img");
let zoomLevel = zoomSlider.value || 100; // Default zoom level at 100%
// Initialize with the default zoom level
updateZoom(zoomLevel, pageCount);
// Slider event listener
zoomSlider.addEventListener("input", function () {
const value = parseInt(zoomSlider.value);
zoomInput.value = value; // Sync the input box with the slider
updateZoom(value, pageCount);
});
// Input event listener
zoomInput.addEventListener("input", function () {
const value = parseInt(zoomInput.value);
if (value >= 60 && value <= 160) {
zoomSlider.value = value; // Sync the slider with the input box
updateZoom(value, pageCount);
} else {
// Reset to last valid zoom level if input is out of bounds
zoomInput.value = zoomLevel;
}
});
}
</script>
</html>