caching system + add extension entries to metadata pool
This commit is contained in:
@@ -4,37 +4,77 @@ let filteredChapters = [];
|
||||
let currentPage = 1;
|
||||
const itemsPerPage = 12;
|
||||
let extensionName = null;
|
||||
let bookSlug = null;
|
||||
|
||||
async function init() {
|
||||
try {
|
||||
const path = window.location.pathname;
|
||||
const parts = path.split("/").filter(Boolean);
|
||||
let bookId;
|
||||
let currentBookId;
|
||||
|
||||
if (parts.length === 3) {
|
||||
extensionName = parts[1];
|
||||
bookId = parts[2];
|
||||
bookSlug = parts[2];
|
||||
|
||||
currentBookId = bookSlug;
|
||||
} else {
|
||||
bookId = parts[1];
|
||||
currentBookId = parts[1];
|
||||
|
||||
}
|
||||
|
||||
const idForFetch = currentBookId;
|
||||
|
||||
const fetchUrl = extensionName
|
||||
? `/api/book/${bookId.slice(0,40)}?ext=${extensionName}`
|
||||
: `/api/book/${bookId}`;
|
||||
? `/api/book/${idForFetch.slice(0,40)}?ext=${extensionName}`
|
||||
: `/api/book/${idForFetch}`;
|
||||
|
||||
const res = await fetch(fetchUrl);
|
||||
const data = await res.json();
|
||||
console.log(data);
|
||||
|
||||
if (data.error) {
|
||||
if (data.error || !data) {
|
||||
const titleEl = document.getElementById('title');
|
||||
if (titleEl) titleEl.innerText = "Book Not Found";
|
||||
return;
|
||||
}
|
||||
|
||||
const title = data.title.english || data.title.romaji;
|
||||
let title, description, score, year, status, format, chapters, poster, banner, genres;
|
||||
|
||||
if (extensionName) {
|
||||
|
||||
title = data.title || data.name || "Unknown";
|
||||
description = data.summary || "No description available.";
|
||||
score = data.score ? Math.round(data.score) : '?';
|
||||
|
||||
year = data.published || '????';
|
||||
|
||||
status = data.status || 'Unknown';
|
||||
format = data.format || 'LN';
|
||||
chapters = data.chapters || '?';
|
||||
poster = data.image || '';
|
||||
|
||||
banner = poster;
|
||||
|
||||
genres = Array.isArray(data.genres) ? data.genres.slice(0, 3).join(' • ') : '';
|
||||
|
||||
} else {
|
||||
|
||||
title = data.title.english || data.title.romaji || "Unknown";
|
||||
description = data.description || "No description available.";
|
||||
score = data.averageScore || '?';
|
||||
|
||||
year = (data.startDate && data.startDate.year) ? data.startDate.year : '????';
|
||||
status = data.status || 'Unknown';
|
||||
format = data.format || 'MANGA';
|
||||
chapters = data.chapters || '?';
|
||||
poster = data.coverImage.extraLarge || data.coverImage.large || '';
|
||||
banner = data.bannerImage || poster;
|
||||
genres = data.genres ? data.genres.slice(0, 3).join(' • ') : '';
|
||||
|
||||
}
|
||||
|
||||
document.title = `${title} | WaifuBoard Books`;
|
||||
|
||||
|
||||
const titleEl = document.getElementById('title');
|
||||
if (titleEl) titleEl.innerText = title;
|
||||
|
||||
@@ -47,71 +87,61 @@ async function init() {
|
||||
}
|
||||
|
||||
const descEl = document.getElementById('description');
|
||||
if (descEl) descEl.innerHTML = data.description || "No description available.";
|
||||
if (descEl) descEl.innerHTML = description;
|
||||
|
||||
const scoreEl = document.getElementById('score');
|
||||
if (scoreEl) scoreEl.innerText = (data.averageScore || '?') + '% Score';
|
||||
if (scoreEl) scoreEl.innerText = score + (extensionName ? '' : '% Score');
|
||||
|
||||
const pubEl = document.getElementById('published-date');
|
||||
if (pubEl) {
|
||||
if (data.startDate && data.startDate.year) {
|
||||
const y = data.startDate.year;
|
||||
const m = data.startDate.month ? `-${data.startDate.month.toString().padStart(2, '0')}` : '';
|
||||
const d = data.startDate.day ? `-${data.startDate.day.toString().padStart(2, '0')}` : '';
|
||||
pubEl.innerText = `${y}${m}${d}`;
|
||||
} else {
|
||||
pubEl.innerText = '????';
|
||||
}
|
||||
}
|
||||
if (pubEl) pubEl.innerText = year;
|
||||
|
||||
const statusEl = document.getElementById('status');
|
||||
if (statusEl) statusEl.innerText = data.status || 'Unknown';
|
||||
if (statusEl) statusEl.innerText = status;
|
||||
|
||||
const formatEl = document.getElementById('format');
|
||||
if (formatEl) formatEl.innerText = data.format || 'MANGA';
|
||||
if (formatEl) formatEl.innerText = format;
|
||||
|
||||
const chaptersEl = document.getElementById('chapters');
|
||||
if (chaptersEl) chaptersEl.innerText = data.chapters || '?';
|
||||
|
||||
if (chaptersEl) chaptersEl.innerText = chapters;
|
||||
|
||||
const genresEl = document.getElementById('genres');
|
||||
if(genresEl && data.genres) {
|
||||
genresEl.innerText = data.genres.slice(0, 3).join(' • ');
|
||||
if(genresEl) {
|
||||
genresEl.innerText = genres;
|
||||
}
|
||||
|
||||
const img = data.coverImage.extraLarge || data.coverImage.large;
|
||||
|
||||
const posterEl = document.getElementById('poster');
|
||||
if (posterEl) posterEl.src = img;
|
||||
if (posterEl) posterEl.src = poster;
|
||||
|
||||
const heroBgEl = document.getElementById('hero-bg');
|
||||
if (heroBgEl) heroBgEl.src = data.bannerImage || img;
|
||||
if (heroBgEl) heroBgEl.src = banner;
|
||||
|
||||
loadChapters();
|
||||
loadChapters(idForFetch);
|
||||
|
||||
} catch (err) {
|
||||
console.error("Metadata Error:", err);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadChapters() {
|
||||
async function loadChapters(idForFetch) {
|
||||
const tbody = document.getElementById('chapters-body');
|
||||
if (!tbody) return;
|
||||
|
||||
|
||||
tbody.innerHTML = '<tr><td colspan="4" style="text-align:center; padding: 2rem;">Searching extensions for chapters...</td></tr>';
|
||||
|
||||
try {
|
||||
|
||||
const fetchUrl = extensionName
|
||||
? `/api/book/${bookId.slice(0, 40)}/chapters`
|
||||
: `/api/book/${bookId}/chapters`;
|
||||
? `/api/book/${idForFetch.slice(0, 40)}/chapters`
|
||||
: `/api/book/${idForFetch}/chapters`;
|
||||
|
||||
const res = await fetch(fetchUrl);
|
||||
const data = await res.json();
|
||||
|
||||
|
||||
allChapters = data.chapters || [];
|
||||
filteredChapters = [...allChapters];
|
||||
|
||||
|
||||
const totalEl = document.getElementById('total-chapters');
|
||||
|
||||
|
||||
if (allChapters.length === 0) {
|
||||
tbody.innerHTML = '<tr><td colspan="4" style="text-align:center; padding: 2rem;">No chapters found on loaded extensions.</td></tr>';
|
||||
if (totalEl) totalEl.innerText = "0 Found";
|
||||
@@ -119,15 +149,16 @@ async function loadChapters() {
|
||||
}
|
||||
|
||||
if (totalEl) totalEl.innerText = `${allChapters.length} Found`;
|
||||
|
||||
|
||||
populateProviderFilter();
|
||||
|
||||
const readBtn = document.getElementById('read-start-btn');
|
||||
if (readBtn && filteredChapters.length > 0) {
|
||||
readBtn.onclick = () => openReader(filteredChapters[0].id);
|
||||
|
||||
readBtn.onclick = () => openReader(idForFetch, filteredChapters[0].id, filteredChapters[0].provider);
|
||||
}
|
||||
|
||||
renderTable();
|
||||
renderTable(idForFetch);
|
||||
|
||||
} catch (err) {
|
||||
tbody.innerHTML = '<tr><td colspan="4" style="text-align:center; color: #ef4444;">Error loading chapters.</td></tr>';
|
||||
@@ -140,12 +171,12 @@ function populateProviderFilter() {
|
||||
if (!select) return;
|
||||
|
||||
const providers = [...new Set(allChapters.map(ch => ch.provider))];
|
||||
|
||||
|
||||
if (providers.length > 0) {
|
||||
select.style.display = 'inline-block';
|
||||
|
||||
|
||||
select.innerHTML = '<option value="all">All Providers</option>';
|
||||
|
||||
|
||||
providers.forEach(prov => {
|
||||
const opt = document.createElement('option');
|
||||
opt.value = prov;
|
||||
@@ -153,6 +184,19 @@ function populateProviderFilter() {
|
||||
select.appendChild(opt);
|
||||
});
|
||||
|
||||
if (extensionName) {
|
||||
|
||||
const extensionProvider = providers.find(p => p.toLowerCase() === extensionName.toLowerCase());
|
||||
|
||||
if (extensionProvider) {
|
||||
|
||||
select.value = extensionProvider;
|
||||
|
||||
filteredChapters = allChapters.filter(ch => ch.provider === extensionProvider);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
select.onchange = (e) => {
|
||||
const selected = e.target.value;
|
||||
if (selected === 'all') {
|
||||
@@ -161,12 +205,13 @@ function populateProviderFilter() {
|
||||
filteredChapters = allChapters.filter(ch => ch.provider === selected);
|
||||
}
|
||||
currentPage = 1;
|
||||
renderTable();
|
||||
const idForFetch = extensionName ? bookSlug : bookId;
|
||||
renderTable(idForFetch);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function renderTable() {
|
||||
function renderTable(idForFetch) {
|
||||
const tbody = document.getElementById('chapters-body');
|
||||
if (!tbody) return;
|
||||
|
||||
@@ -206,25 +251,27 @@ function renderTable() {
|
||||
function updatePagination() {
|
||||
const totalPages = Math.ceil(filteredChapters.length / itemsPerPage);
|
||||
const pagination = document.getElementById('pagination');
|
||||
|
||||
|
||||
if (!pagination) return;
|
||||
|
||||
if (totalPages <= 1) {
|
||||
pagination.style.display = 'none';
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
pagination.style.display = 'flex';
|
||||
document.getElementById('page-info').innerText = `Page ${currentPage} of ${totalPages}`;
|
||||
|
||||
|
||||
const prevBtn = document.getElementById('prev-page');
|
||||
const nextBtn = document.getElementById('next-page');
|
||||
|
||||
|
||||
prevBtn.disabled = currentPage === 1;
|
||||
nextBtn.disabled = currentPage >= totalPages;
|
||||
|
||||
prevBtn.onclick = () => { currentPage--; renderTable(); };
|
||||
nextBtn.onclick = () => { currentPage++; renderTable(); };
|
||||
const idForFetch = extensionName ? bookSlug : bookId;
|
||||
|
||||
prevBtn.onclick = () => { currentPage--; renderTable(idForFetch); };
|
||||
nextBtn.onclick = () => { currentPage++; renderTable(idForFetch); };
|
||||
}
|
||||
|
||||
function openReader(bookId, chapterId, provider) {
|
||||
|
||||
Reference in New Issue
Block a user