-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (131 loc) · 5.02 KB
/
script.js
File metadata and controls
156 lines (131 loc) · 5.02 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
const CONFIG = {
chapterPrefix: 'chapter-',
chapterExtension: '.txt',
basePath: './chapters/',
totalChapters: 6,
storageKey: 'lastChapter'
};
const cache = new Map();
let currentChapter = null;
const el = {
select: document.getElementById('chapterSelect'),
prevBtn: document.getElementById('prevBtn'),
nextBtn: document.getElementById('nextBtn'),
menuBtn: document.getElementById('menuBtn'),
settingsModal: document.getElementById('settingsModal'),
modalContainer: document.getElementById('modalContainer'),
closeModal: document.getElementById('closeModal'),
modalOverlay: document.getElementById('modalOverlay'),
goToInput: document.getElementById('goToInput'),
goToBtn: document.getElementById('goToBtn'),
content: document.getElementById('content'),
readingProgress: document.getElementById('readingProgress'),
statusToast: document.getElementById('statusToast')
};
function parseChapterNum(val) {
const num = parseInt(val, 10);
return isNaN(num) ? null : num;
}
function isValidChapter(num) {
return num !== null && num >= 1 && num <= CONFIG.totalChapters;
}
function showToast(message) {
el.statusToast.textContent = message;
el.statusToast.classList.remove('opacity-0', 'scale-95');
setTimeout(() => el.statusToast.classList.add('opacity-0', 'scale-95'), 2000);
}
function toggleModal(show) {
if (show) {
el.settingsModal.classList.remove('pointer-events-none', 'opacity-0');
el.modalContainer.classList.remove('scale-95');
el.modalContainer.classList.add('scale-100');
} else {
el.settingsModal.classList.add('pointer-events-none', 'opacity-0');
el.modalContainer.classList.remove('scale-100');
el.modalContainer.classList.add('scale-95');
}
}
async function loadChapter(num) {
num = parseChapterNum(num);
if (!isValidChapter(num) || num === currentChapter) return;
currentChapter = num;
el.select.value = currentChapter;
el.readingProgress.textContent = `Chapter ${currentChapter} / ${CONFIG.totalChapters}`;
el.prevBtn.disabled = currentChapter <= 1;
el.nextBtn.disabled = currentChapter >= CONFIG.totalChapters;
history.replaceState(null, '', '#' + currentChapter);
localStorage.setItem(CONFIG.storageKey, currentChapter);
if (cache.has(currentChapter)) {
renderContent(cache.get(currentChapter));
return;
}
el.content.classList.add('opacity-20');
try {
const response = await fetch(`${CONFIG.basePath}${CONFIG.chapterPrefix}${currentChapter}${CONFIG.chapterExtension}`);
if (!response.ok) throw new Error('404');
const text = await response.text();
cache.set(currentChapter, text);
if (currentChapter === num) renderContent(text);
} catch (err) {
el.content.innerHTML = `<p class="text-center py-20 text-slate-600 uppercase tracking-widest text-xs">This chapter cannot be reached in the archive.</p>`;
} finally {
el.content.classList.remove('opacity-20');
}
}
function renderContent(text) {
el.content.textContent = text;
window.scrollTo({ top: 0, behavior: 'instant' });
lucide.createIcons();
}
function setupDropdown() {
const fragment = document.createDocumentFragment();
for (let i = 1; i <= CONFIG.totalChapters; i++) {
const option = document.createElement('option');
option.value = i;
option.textContent = `Chapter ${i}`;
fragment.appendChild(option);
}
el.select.appendChild(fragment);
}
el.menuBtn.onclick = () => toggleModal(true);
el.closeModal.onclick = () => toggleModal(false);
el.modalOverlay.onclick = () => toggleModal(false);
el.select.onchange = (e) => {
loadChapter(e.target.value);
toggleModal(false);
};
el.goToBtn.onclick = () => {
const val = parseChapterNum(el.goToInput.value);
if (isValidChapter(val)) {
loadChapter(val);
toggleModal(false);
el.goToInput.value = '';
} else {
showToast("Out of Range!");
}
};
el.prevBtn.onclick = () => loadChapter(currentChapter - 1);
el.nextBtn.onclick = () => loadChapter(currentChapter + 1);
document.addEventListener('keydown', (e) => {
if (e.target.tagName === 'INPUT' || e.target.tagName === 'SELECT') return;
if (e.key === 'ArrowLeft') loadChapter(currentChapter - 1);
if (e.key === 'ArrowRight') loadChapter(currentChapter + 1);
if (e.key === 'Escape') toggleModal(false);
if (e.key.toLowerCase() === 'm') toggleModal(true);
});
window.addEventListener('hashchange', () => {
const num = parseChapterNum(window.location.hash.substring(1));
if (isValidChapter(num) && num !== currentChapter) {
loadChapter(num);
}
});
function init() {
setupDropdown();
lucide.createIcons();
const hashNum = parseChapterNum(window.location.hash.substring(1));
const lastRead = parseChapterNum(localStorage.getItem(CONFIG.storageKey));
if (isValidChapter(hashNum)) loadChapter(hashNum);
else if (isValidChapter(lastRead)) loadChapter(lastRead);
else loadChapter(1);
}
init();