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) {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
let trendingBooks = [];
|
||||
let currentHeroIndex = 0;
|
||||
let heroInterval;
|
||||
let availableExtensions = [];
|
||||
|
||||
window.addEventListener('scroll', () => {
|
||||
const nav = document.getElementById('navbar');
|
||||
@@ -15,7 +16,7 @@ let searchTimeout;
|
||||
searchInput.addEventListener('input', (e) => {
|
||||
const query = e.target.value;
|
||||
clearTimeout(searchTimeout);
|
||||
|
||||
|
||||
if (query.length < 2) {
|
||||
searchResults.classList.remove('active');
|
||||
searchResults.innerHTML = '';
|
||||
@@ -37,25 +38,51 @@ document.addEventListener('click', (e) => {
|
||||
|
||||
async function fetchBookSearch(query) {
|
||||
try {
|
||||
const res = await fetch(`/api/search/books?q=${encodeURIComponent(query)}`);
|
||||
let apiUrl = `/api/search/books?q=${encodeURIComponent(query)}`;
|
||||
let extensionName = null;
|
||||
let finalQuery = query;
|
||||
|
||||
const parts = query.split(':');
|
||||
if (parts.length >= 2) {
|
||||
const potentialExtension = parts[0].trim().toLowerCase();
|
||||
|
||||
const foundExtension = availableExtensions.find(ext => ext.toLowerCase() === potentialExtension);
|
||||
|
||||
if (foundExtension) {
|
||||
extensionName = foundExtension;
|
||||
|
||||
finalQuery = parts.slice(1).join(':').trim();
|
||||
|
||||
if (finalQuery.length === 0) {
|
||||
renderSearchResults([]);
|
||||
return;
|
||||
}
|
||||
|
||||
apiUrl = `/api/search/books/${extensionName}?q=${encodeURIComponent(finalQuery)}`;
|
||||
}
|
||||
}
|
||||
|
||||
const res = await fetch(apiUrl);
|
||||
const data = await res.json();
|
||||
renderSearchResults(data.results || []);
|
||||
} catch (err) {
|
||||
console.error("Search Error:", err);
|
||||
|
||||
const resultsWithExtension = data.results.map(book => {
|
||||
if (extensionName) {
|
||||
return { ...book,
|
||||
isExtensionResult: true,
|
||||
extensionName: extensionName
|
||||
|
||||
};
|
||||
}
|
||||
return book;
|
||||
});
|
||||
|
||||
renderSearchResults(resultsWithExtension || []);
|
||||
} catch (err) {
|
||||
console.error("Search Error:", err);
|
||||
renderSearchResults([]);
|
||||
}
|
||||
}
|
||||
|
||||
function createSlug(text) {
|
||||
if (!text) return '';
|
||||
return text
|
||||
.toLowerCase()
|
||||
.trim()
|
||||
.replace(/-/g, '--')
|
||||
.replace(/\s+/g, '-')
|
||||
.replace(/[^a-z0-9\-]/g, '');
|
||||
}
|
||||
|
||||
function renderSearchResults(results) {
|
||||
searchResults.innerHTML = '';
|
||||
|
||||
@@ -65,25 +92,18 @@ function renderSearchResults(results) {
|
||||
results.forEach(book => {
|
||||
const title = book.title.english || book.title.romaji || "Unknown";
|
||||
const img = (book.coverImage && (book.coverImage.medium || book.coverImage.large)) || '';
|
||||
const rating = book.averageScore ? `${book.averageScore}%` : 'N/A';
|
||||
const rating = Number.isInteger(book.averageScore) ? `${book.averageScore}%` : book.averageScore || 'N/A';
|
||||
const year = book.seasonYear || (book.startDate ? book.startDate.year : '') || '????';
|
||||
const format = book.format || 'MANGA';
|
||||
let href;
|
||||
|
||||
if (book.isExtensionResult) {
|
||||
const titleSlug = createSlug(title);
|
||||
href = `/book/${book.extensionName}/${titleSlug}`;
|
||||
} else {
|
||||
href = `/book/${book.id}`;
|
||||
}
|
||||
|
||||
const extName = book.extensionName.charAt(0).toUpperCase() + book.extensionName.slice(1);
|
||||
const extPill = book.isExtensionResult
|
||||
? `<span>${extName}</span>`
|
||||
: '';
|
||||
|
||||
const item = document.createElement('a');
|
||||
item.className = 'search-item';
|
||||
let href;
|
||||
if (book.isExtensionResult) {
|
||||
href = `/book/${book.extensionName}/${book.id}`;
|
||||
} else {
|
||||
href = `/book/${book.id}`;
|
||||
}
|
||||
item.href = href;
|
||||
|
||||
item.innerHTML = `
|
||||
@@ -94,7 +114,6 @@ function renderSearchResults(results) {
|
||||
<span class="rating-pill">${rating}</span>
|
||||
<span>• ${year}</span>
|
||||
<span>• ${format}</span>
|
||||
${extPill}
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
@@ -117,9 +136,17 @@ function scrollCarousel(id, direction) {
|
||||
|
||||
async function init() {
|
||||
try {
|
||||
|
||||
const resExt = await fetch('/api/extensions/book');
|
||||
const dataExt = await resExt.json();
|
||||
if (dataExt.extensions) {
|
||||
availableExtensions = dataExt.extensions;
|
||||
console.log("Extensiones disponibles cargadas:", availableExtensions);
|
||||
}
|
||||
|
||||
const res = await fetch('/api/books/trending');
|
||||
const data = await res.json();
|
||||
|
||||
|
||||
if (data.results && data.results.length > 0) {
|
||||
trendingBooks = data.results;
|
||||
updateHeroUI(trendingBooks[0]);
|
||||
@@ -152,21 +179,22 @@ function updateHeroUI(book) {
|
||||
const desc = book.description || "No description available.";
|
||||
const poster = (book.coverImage && (book.coverImage.extraLarge || book.coverImage.large)) || '';
|
||||
const banner = book.bannerImage || poster;
|
||||
|
||||
|
||||
document.getElementById('hero-title').innerText = title;
|
||||
document.getElementById('hero-desc').innerHTML = desc;
|
||||
document.getElementById('hero-score').innerText = (book.averageScore || '?') + '% Score';
|
||||
document.getElementById('hero-year').innerText = (book.startDate && book.startDate.year) ? book.startDate.year : '????';
|
||||
document.getElementById('hero-type').innerText = book.format || 'MANGA';
|
||||
|
||||
|
||||
const heroPoster = document.getElementById('hero-poster');
|
||||
if(heroPoster) heroPoster.src = poster;
|
||||
|
||||
|
||||
const bg = document.getElementById('hero-bg-media');
|
||||
if(bg) bg.src = banner;
|
||||
|
||||
const readBtn = document.getElementById('read-btn');
|
||||
if (readBtn) {
|
||||
|
||||
readBtn.onclick = () => window.location.href = `/book/${book.id}`;
|
||||
}
|
||||
}
|
||||
@@ -174,7 +202,7 @@ function updateHeroUI(book) {
|
||||
function renderList(id, list) {
|
||||
const container = document.getElementById(id);
|
||||
container.innerHTML = '';
|
||||
|
||||
|
||||
list.forEach(book => {
|
||||
const title = book.title.english || book.title.romaji;
|
||||
const cover = book.coverImage ? book.coverImage.large : '';
|
||||
@@ -183,6 +211,7 @@ function renderList(id, list) {
|
||||
|
||||
const el = document.createElement('div');
|
||||
el.className = 'card';
|
||||
|
||||
el.onclick = () => {
|
||||
window.location.href = `/book/${book.id}`;
|
||||
};
|
||||
|
||||
@@ -467,7 +467,7 @@ nextBtn.addEventListener('click', () => {
|
||||
|
||||
function updateURL(newChapter) {
|
||||
chapter = newChapter;
|
||||
const newUrl = `/reader/${provider}/${chapter}/${bookId}`;
|
||||
const newUrl = `/read/${provider}/${chapter}/${bookId}`;
|
||||
window.history.pushState({}, '', newUrl);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user