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

@@ -15,7 +15,6 @@ tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
// Auth helpers
function getAuthToken() {
return localStorage.getItem('token');
}
@@ -28,32 +27,46 @@ function getAuthHeaders() {
};
}
// Check if anime is in list
function getSimpleAuthHeaders() {
const token = getAuthToken();
return {
'Authorization': `Bearer ${token}`
};
}
async function checkIfInList() {
const entryId = window.location.pathname.split('/').pop();
const source = extensionName || 'anilist';
const entryType = 'ANIME';
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 entry = data.results?.find(item =>
item.entry_id === parseInt(animeId) &&
item.source === (extensionName || 'anilist')
);
if (entry) {
if (data.found && data.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 (isInList) {
@@ -68,13 +81,15 @@ function updateAddToListButton() {
btn.style.borderColor = 'rgba(34, 197, 94, 0.3)';
} else {
btn.innerHTML = '+ Add to List';
btn.style.background = null;
btn.style.color = null;
btn.style.borderColor = null;
}
}
// Open add to list modal
function openAddToListModal() {
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 || '';
@@ -82,7 +97,7 @@ function openAddToListModal() {
document.getElementById('modal-title').textContent = 'Edit List 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 = '';
@@ -94,12 +109,10 @@ function openAddToListModal() {
document.getElementById('modal-progress').max = totalEpisodes || 999;
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;
@@ -123,12 +136,12 @@ async function saveToList() {
throw new Error('Failed to save entry');
}
// Si la operación fue exitosa, actualizamos currentListEntry con el nuevo campo type
isInList = true;
currentListEntry = {
entry_id: parseInt(animeId),
source: extensionName || 'anilist',
entry_type: 'ANIME', // <--- También se actualiza aquí
entry_type: 'ANIME',
status,
progress,
score
@@ -142,14 +155,14 @@ async function saveToList() {
}
}
// Delete from list
async function deleteFromList() {
if (!confirm('Remove this anime from your list?')) return;
try {
const response = await fetch(`${API_BASE}/list/entry/${animeId}`, {
method: 'DELETE',
headers: getAuthHeaders()
headers: getSimpleAuthHeaders()
});
if (!response.ok) {
@@ -167,7 +180,6 @@ async function deleteFromList() {
}
}
// Show notification
function showNotification(message, type = 'info') {
const notification = document.createElement('div');
notification.style.cssText = `
@@ -208,7 +220,7 @@ async function loadAnime() {
const fetchUrl = extensionName
? `/api/anime/${animeId}?source=${extensionName}`
: `/api/anime/${animeId}?source=anilist`;
const res = await fetch(fetchUrl);
const res = await fetch(fetchUrl, { headers: getSimpleAuthHeaders() });
const data = await res.json();
if (data.error) {
@@ -362,7 +374,6 @@ async function loadAnime() {
renderEpisodes();
// Check if in list after loading anime data
await checkIfInList();
} catch (err) {
@@ -458,7 +469,6 @@ searchInput.addEventListener('input', (e) => {
}
});
// Close modal on outside click
document.addEventListener('DOMContentLoaded', () => {
const modal = document.getElementById('add-list-modal');
if (modal) {
@@ -470,7 +480,6 @@ document.addEventListener('DOMContentLoaded', () => {
}
});
// Add animations
const style = document.createElement('style');
style.textContent = `
@keyframes slideInRight {