codebydante's picture
Subida inicial de detector de enlaces - copia en la raíz del repositorio
596aae4 verified
Raw History Blame Contribute Delete
8.6 kB
import json
import re
with open('popup.js', 'r', encoding='utf-8') as f:
content = f.read()
start_idx = content.find(' // Toggle Logic para Kemono Descargas')
end_idx = content.find(' // Toggle Logic para Facebook')
if start_idx != -1 and end_idx != -1:
downloads_block = content[start_idx:end_idx]
images_block = downloads_block.replace('toggleKemonoDownloads', 'toggleKemonoImages')
images_block = images_block.replace('__kemonoDownloadsActive', '__kemonoImagesActive')
images_block = images_block.replace('kemono-dl-', 'kemono-img-')
images_block = images_block.replace('#fbbf24', '#14b8a6')
images_block = images_block.replace('#f59e0b', '#0d9488')
images_block = images_block.replace('¡Guardado en Descargas!', '¡Enviado a Descargar Imágenes!')
extraction_start = images_block.find('let outputWithLinks =')
extraction_end = images_block.find('document.body.removeChild(iframe);')
new_extraction = '''let extractionPayload = [];
let count = 0;
const CONCURRENCY_LIMIT = 4;
let queue = [...uniqueUrls];
const extractWorker = async (workerId) => {
let iframe = document.createElement('iframe');
iframe.id = `kemono-extractor-frame-${workerId}`;
iframe.style.width = '1px';
iframe.style.height = '1px';
iframe.style.opacity = '0';
iframe.style.pointerEvents = 'none';
iframe.style.position = 'fixed';
iframe.style.bottom = '0';
document.body.appendChild(iframe);
while (queue.length > 0) {
const url = queue.shift();
count++;
copyBtn.textContent = `Analizando páginas... ${Math.round((count/total)*100)}%`;
try {
let loaded = false;
iframe.onload = () => { loaded = true; };
iframe.src = url;
let waitTimer = 0;
while (!loaded && waitTimer < 15000) {
await new Promise(r => setTimeout(r, 200));
waitTimer += 200;
}
await new Promise(r => setTimeout(r, 2500 + Math.random() * 500));
const doc = iframe.contentDocument;
if (!doc) throw new Error("Página inaccesible");
let titleEl = doc.querySelector('.post__title span') || doc.querySelector('.post__title') || doc.querySelector('h1');
let title = titleEl ? titleEl.innerText.trim() : 'Desconocido';
let authorEl = doc.querySelector('.post__user-name') || doc.querySelector('.post__author');
let author = authorEl ? authorEl.innerText.trim() : 'Desconocido';
let imageLinks = [];
const isImage = (str) => /\.(jpg|jpeg|png|webp|gif|avif)(\?|$|\w)/i.test(str || '');
// Attachments
doc.querySelectorAll('a.post__attachment-link, .post__attachment a').forEach(a => {
let href = a.getAttribute('href');
let name = a.getAttribute('download') || a.innerText.trim();
if (isImage(href) || isImage(name)) {
if (!href.startsWith('http')) href = new URL(href, url).href;
imageLinks.push(href);
}
});
// Images inside content
doc.querySelectorAll('.post__content img, .post__body img, .post__content a, .post__body a').forEach(el => {
let src = el.getAttribute('src') || el.getAttribute('href');
if (isImage(src)) {
if (!src.startsWith('http')) src = new URL(src, url).href;
imageLinks.push(src);
}
});
imageLinks = [...new Set(imageLinks)];
if (imageLinks.length > 0) {
extractionPayload.push({
url: url,
title: title,
author: author,
images: imageLinks
});
try {
const urlPath = new URL(url).pathname;
const cardAnchor = document.querySelector(`.post-card a[href="${urlPath}"]`) || document.querySelector(`.post-card a[href$="${urlPath}"]`);
if (cardAnchor) {
const card = cardAnchor.closest('.post-card');
if (card) {
card.classList.add('kemono-img-success');
setTimeout(() => card.classList.remove('kemono-img-success'), 150);
setTimeout(() => card.classList.add('kemono-img-success'), 300);
}
}
} catch(e) {}
}
} catch (err) {
console.error("Error cargando " + url, err);
}
}
document.body.removeChild(iframe);
};'''
images_block = images_block[:extraction_start] + new_extraction + images_block[extraction_end + len('document.body.removeChild(iframe);'):]
images_block = images_block.replace('type: "SAVE_KEMONO"', 'type: "DOWNLOAD_IMAGES_KEMONO"')
images_block = re.sub(r'textLinks:.*,', 'data: extractionPayload', images_block)
images_block = re.sub(r'textNoLinks:.*', '', images_block)
images_block = images_block.replace('// Toggle Logic para Kemono Descargas', '// Toggle Logic para Kemono Imagenes')
images_block = images_block.replace('Procesar Enlaces', 'Procesar Imágenes')
new_content = content[:end_idx] + images_block + '\n' + content[end_idx:]
with open('popup.js', 'w', encoding='utf-8') as f:
f.write(new_content)
print("Injection successful.")
else:
print("Could not find blocks")