File size: 8,596 Bytes
e9264a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
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")