Implement download logic in content script
✅ Now works without popup:
- downloadSelectedMangas() downloads selected manga
- downloadAllMangas() downloads all manga from page
- Progress updates in control panel
- Download happens directly in content script
- No need to keep popup open
- Buttons in control panel now work!
Features:
- Real-time progress bar
- Live status updates
- Automatic completion handling
- Error logging for failed downloads
- Returns to control panel after completion
This commit is contained in:
107
content.js
107
content.js
@@ -251,12 +251,11 @@
|
||||
sendResponse({ success: true });
|
||||
} else if (request.action === 'triggerDownloadSelected') {
|
||||
console.log('🎯 Trigger: Descargar Seleccionados');
|
||||
// Enviar mensaje al popup.js para ejecutar la descarga
|
||||
chrome.runtime.sendMessage({ action: 'executeDownloadSelected' });
|
||||
downloadSelectedMangas();
|
||||
sendResponse({ success: true });
|
||||
} else if (request.action === 'triggerDownloadAll') {
|
||||
console.log('🎯 Trigger: Descargar TODOS');
|
||||
chrome.runtime.sendMessage({ action: 'executeDownloadAll' });
|
||||
downloadAllMangas();
|
||||
sendResponse({ success: true });
|
||||
} else {
|
||||
console.log('❓ Acción desconocida:', request.action);
|
||||
@@ -589,6 +588,108 @@
|
||||
document.body.appendChild(button);
|
||||
}
|
||||
|
||||
// Descargar manga seleccionados
|
||||
async function downloadSelectedMangas() {
|
||||
console.log('📥 Iniciando descarga de manga seleccionados...');
|
||||
|
||||
const selectedObjects = [];
|
||||
selectedMangas.forEach(id => {
|
||||
const mangaObj = mangaMetadata.get(id);
|
||||
if (mangaObj) {
|
||||
selectedObjects.push(mangaObj);
|
||||
}
|
||||
});
|
||||
|
||||
if (selectedObjects.length === 0) {
|
||||
alert('No hay manga seleccionados. Selecciona algunos manga primero.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`📦 Manga seleccionados: ${selectedObjects.length}`);
|
||||
|
||||
// Descargar cada manga
|
||||
for (let i = 0; i < selectedObjects.length; i++) {
|
||||
const manga = selectedObjects[i];
|
||||
const title = manga.title ? manga.title.substring(0, 50) : 'Manga sin título';
|
||||
|
||||
updateProgress(i, selectedObjects.length, title, `Descargando manga ${i + 1} de ${selectedObjects.length}...`);
|
||||
|
||||
try {
|
||||
// Obtener URLs de imágenes
|
||||
const imageUrls = await getImageUrlsForManga(manga);
|
||||
console.log(`✅ ${manga.title}: ${imageUrls.length} imágenes encontradas`);
|
||||
|
||||
// Enviar al background para descarga
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
action: 'downloadManga',
|
||||
metadata: manga,
|
||||
imageUrls: imageUrls
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
console.error(`❌ Error descargando ${manga.title}:`, response.error);
|
||||
} else {
|
||||
console.log(`✅ Descargado: ${manga.title}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error en manga ${manga.title}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
updateProgress(selectedObjects.length, selectedObjects.length, '¡Completado!', 'Descarga finalizada');
|
||||
setTimeout(() => {
|
||||
hideProgress();
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
// Descargar TODOS los manga de la página
|
||||
async function downloadAllMangas() {
|
||||
console.log('📥 Iniciando descarga de TODOS los manga...');
|
||||
|
||||
const allMangas = extractAllMangasFromPage();
|
||||
|
||||
if (allMangas.length === 0) {
|
||||
alert('No se encontraron manga en esta página.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log(`📦 Manga encontrados: ${allMangas.length}`);
|
||||
|
||||
// Descargar cada manga
|
||||
for (let i = 0; i < allMangas.length; i++) {
|
||||
const manga = allMangas[i];
|
||||
const title = manga.title ? manga.title.substring(0, 50) : 'Manga sin título';
|
||||
|
||||
updateProgress(i, allMangas.length, title, `Descargando manga ${i + 1} de ${allMangas.length}...`);
|
||||
|
||||
try {
|
||||
// Obtener URLs de imágenes
|
||||
const imageUrls = await getImageUrlsForManga(manga);
|
||||
console.log(`✅ ${manga.title}: ${imageUrls.length} imágenes encontradas`);
|
||||
|
||||
// Enviar al background para descarga
|
||||
const response = await chrome.runtime.sendMessage({
|
||||
action: 'downloadManga',
|
||||
metadata: manga,
|
||||
imageUrls: imageUrls
|
||||
});
|
||||
|
||||
if (!response.success) {
|
||||
console.error(`❌ Error descargando ${manga.title}:`, response.error);
|
||||
} else {
|
||||
console.log(`✅ Descargado: ${manga.title}`);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`❌ Error en manga ${manga.title}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
updateProgress(allMangas.length, allMangas.length, '¡Completado!', 'Descarga finalizada');
|
||||
setTimeout(() => {
|
||||
hideProgress();
|
||||
}, 2000);
|
||||
}
|
||||
|
||||
function init() {
|
||||
if (window.location.href.includes('/g/')) return;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user