anilist integrated to my list

This commit is contained in:
2025-12-06 17:18:03 +01:00
parent e5ec8aa7e5
commit 822a9f83cf
13 changed files with 774 additions and 257 deletions

View File

@@ -6,7 +6,6 @@ const itemsPerPage = 12;
let extensionName = null;
let bookSlug = null;
// NUEVAS VARIABLES GLOBALES PARA LISTA
let currentBookData = null;
let isInList = false;
let currentListEntry = null;
@@ -21,11 +20,6 @@ function getChaptersUrl(id, source = 'anilist') {
return `/api/book/${id}/chapters?source=${source}`;
}
// ==========================================================
// 1. FUNCIONES DE AUTENTICACIÓN Y LISTA
// ==========================================================
// Auth helpers
function getAuthToken() {
return localStorage.getItem('token');
}
@@ -38,34 +32,53 @@ function getAuthHeaders() {
};
}
// Check if book is in list
function getSimpleAuthHeaders() {
const token = getAuthToken();
return {
'Authorization': `Bearer ${token}`
};
}
function getBookEntryType(bookData) {
if (!bookData) return 'MANGA';
const format = bookData.format?.toUpperCase() || 'MANGA';
return (format === 'MANGA' || format === 'ONE_SHOT' || format === 'MANHWA') ? 'MANGA' : 'NOVEL';
}
async function checkIfInList() {
if (!currentBookData) return;
const entryId = extensionName ? bookSlug : bookId;
const source = extensionName || 'anilist';
const entryType = getBookEntryType(currentBookData);
const fetchUrl = `${API_BASE}/list/entry/${entryId}?source=${source}&entry_type=${entryType}`;
try {
const response = await fetch(`${API_BASE}/list`, {
headers: getAuthHeaders()
const response = await fetch(fetchUrl, {
headers: getSimpleAuthHeaders()
});
if (response.ok) {
const data = await response.json();
const idToSearch = extensionName ? bookSlug : bookId;
const entry = data.results?.find(item =>
item.entry_id === idToSearch &&
item.source === (extensionName || 'anilist')
);
if (data.found && data.entry) {
if (entry) {
isInList = true;
currentListEntry = entry;
updateAddToListButton();
currentListEntry = data.entry;
} else {
isInList = false;
currentListEntry = null;
}
updateAddToListButton();
}
} catch (error) {
console.error('Error checking list:', error);
console.error('Error checking single list entry:', error);
}
}
// Update button state
function updateAddToListButton() {
const btn = document.getElementById('add-to-list-btn');
if (!btn) return;
@@ -83,22 +96,20 @@ function updateAddToListButton() {
btn.onclick = openAddToListModal;
} else {
btn.innerHTML = '+ Add to Library';
btn.style.background = null; // Restablecer estilos si no está en lista
btn.style.background = null;
btn.style.color = null;
btn.style.borderColor = null;
btn.onclick = openAddToListModal;
}
}
// Open add to list modal
function openAddToListModal() {
if (!currentBookData) return;
// Obtener el total de capítulos/volúmenes
const totalUnits = currentBookData.chapters || currentBookData.volumes || 999;
if (isInList) {
// If already in list, open edit modal with current data
document.getElementById('modal-status').value = currentListEntry.status || 'PLANNING';
document.getElementById('modal-progress').value = currentListEntry.progress || 0;
document.getElementById('modal-score').value = currentListEntry.score || '';
@@ -106,7 +117,7 @@ function openAddToListModal() {
document.getElementById('modal-title').textContent = 'Edit Library Entry';
document.getElementById('modal-delete-btn').style.display = 'block';
} else {
// New entry defaults
document.getElementById('modal-status').value = 'PLANNING';
document.getElementById('modal-progress').value = 0;
document.getElementById('modal-score').value = '';
@@ -115,7 +126,6 @@ function openAddToListModal() {
document.getElementById('modal-delete-btn').style.display = 'none';
}
// Ajustar etiqueta de progreso según el formato
const progressLabel = document.getElementById('modal-progress-label');
if (progressLabel) {
const format = currentBookData.format?.toUpperCase() || 'MANGA';
@@ -130,12 +140,10 @@ function openAddToListModal() {
document.getElementById('add-list-modal').classList.add('active');
}
// Close modal
function closeAddToListModal() {
document.getElementById('add-list-modal').classList.remove('active');
}
// Save to list
async function saveToList() {
const status = document.getElementById('modal-status').value;
const progress = parseInt(document.getElementById('modal-progress').value) || 0;
@@ -146,9 +154,7 @@ async function saveToList() {
return;
}
// Determinar el tipo de entrada (MANGA o NOVEL basado en el formato)
const format = currentBookData.format?.toUpperCase() || 'MANGA';
const entryType = (format === 'MANGA' || format === 'ONE_SHOT' || format === 'MANHWA') ? 'MANGA' : 'NOVEL';
const entryType = getBookEntryType(currentBookData);
const idToSave = extensionName ? bookSlug : bookId;
try {
@@ -180,7 +186,6 @@ async function saveToList() {
}
}
// Delete from list
async function deleteFromList() {
if (!confirm('Remove this book from your library?')) return;
@@ -189,7 +194,8 @@ async function deleteFromList() {
try {
const response = await fetch(`${API_BASE}/list/entry/${idToDelete}`, {
method: 'DELETE',
headers: getAuthHeaders()
headers: getSimpleAuthHeaders()
});
if (!response.ok) {
@@ -207,7 +213,6 @@ async function deleteFromList() {
}
}
// Show notification
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.style.cssText = `
@@ -232,11 +237,6 @@ function showNotification(message, type = 'info') {
}, 3000);
}
// ==========================================================
// 2. FUNCIÓN PRINCIPAL DE CARGA (init)
// ==========================================================
async function init() {
try {
const path = window.location.pathname;
@@ -260,7 +260,7 @@ async function init() {
extensionName || 'anilist'
);
const res = await fetch(fetchUrl);
const res = await fetch(fetchUrl, { headers: getSimpleAuthHeaders() });
const data = await res.json();
if (data.error || !data) {
@@ -269,7 +269,7 @@ async function init() {
return;
}
currentBookData = data; // <--- GUARDAR DATOS GLOBALES
currentBookData = data;
let title, description, score, year, status, format, chapters, poster, banner, genres;
@@ -350,18 +350,13 @@ async function init() {
loadChapters(idForFetch);
await checkIfInList(); // <--- COMPROBAR ESTADO DE LA LISTA
await checkIfInList();
} catch (err) {
console.error("Metadata Error:", err);
}
}
// ==========================================================
// 3. FUNCIONES DE CARGA Y RENDERIZADO DE CAPÍTULOS
// (Sin cambios, pero se incluyen para completar el script)
// ==========================================================
async function loadChapters(idForFetch) {
const tbody = document.getElementById('chapters-body');
if (!tbody) return;
@@ -375,7 +370,7 @@ async function loadChapters(idForFetch) {
extensionName || 'anilist'
);
const res = await fetch(fetchUrl);
const res = await fetch(fetchUrl, { headers: getSimpleAuthHeaders() });
const data = await res.json();
allChapters = data.chapters || [];
@@ -523,11 +518,6 @@ function openReader(bookId, chapterId, provider) {
window.location.href = `/read/${p}/${c}/${bookId}${extension}`;
}
// ==========================================================
// 4. LISTENERS Y ARRANQUE
// ==========================================================
// Close modal on outside click
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById('add-list-modal');
if (modal) {
@@ -539,7 +529,6 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
// Add animations (Copied from anime.js)
const style = document.createElement('style');
style.textContent = `
@keyframes slideInRight {