File size: 4,543 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
let scanState = {
    isScanning: false,
    scannedCount: 0,
    totalCount: 0,
    foundUrls: [],
    albumUrls: [],
    error: null
};

chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
    if (message.type === 'GET_STATE') {
        sendResponse(scanState);
        return true;
    }
    
    if (message.type === 'START_SCAN') {
        if (!scanState.isScanning) {
            startScan(message.payload.albumUrls, message.payload.currentVideos);
        }
        sendResponse(scanState);
        return true;
    }

    if (message.type === 'RESET_SCAN') {
        if (!scanState.isScanning) {
            scanState = {
                isScanning: false,
                scannedCount: 0,
                totalCount: 0,
                foundUrls: [],
                albumUrls: [],
                error: null
            };
        }
        sendResponse(scanState);
        return true;
    }
});

async function startScan(albumUrls, initialVideos) {
    scanState.isScanning = true;
    scanState.albumUrls = albumUrls;
    scanState.totalCount = albumUrls.length;
    scanState.scannedCount = 0;
    scanState.foundUrls = Array.from(new Set(initialVideos || []));
    scanState.error = null;

    chrome.runtime.sendMessage({ type: 'SCAN_UPDATE', payload: scanState }).catch(() => {});

    for (const url of albumUrls) {
        scanState.scannedCount++;
        try {
            let res = await fetch(url);
            let text = await res.text();
            
            let newUrls = new Set();
            let srcMatches = text.matchAll(/src=["'](https?:\/\/[^"'\s]+\.mp4)['"]/gi);
            for (const match of srcMatches) {
                newUrls.add(match[1]);
            }
            
            let hrefMatches = text.matchAll(/href=["'](https?:\/\/[^"'\s]+\.mp4)['"]/gi);
            for (const match of hrefMatches) {
                newUrls.add(match[1]);
            }

            for (const u of newUrls) {
                if (!scanState.foundUrls.includes(u)) {
                    scanState.foundUrls.push(u);
                }
            }
        } catch (e) {
            console.error("Error fetching album", url, e);
        }
        
        chrome.runtime.sendMessage({ type: 'SCAN_UPDATE', payload: scanState }).catch(() => {});
    }

    scanState.isScanning = false;
    chrome.runtime.sendMessage({ type: 'SCAN_UPDATE', payload: scanState }).catch(() => {});
}

// Proxy for Native Messaging (content scripts -> native host)
chrome.runtime.onConnect.addListener((port) => {
    if (port.name === "kemono-native-proxy") {
        let nativePort = null;
        
        port.onMessage.addListener((msg) => {
            if (msg.type === "EXTRACT_KEMONO" || msg.type === "SAVE_KEMONO" || msg.type === "DOWNLOAD_IMAGES_KEMONO") {
                try {
                    nativePort = chrome.runtime.connectNative("com.linkdetector.kemono");
                    
                    nativePort.onMessage.addListener((nativeMsg) => {
                        port.postMessage(nativeMsg);
                    });
                    
                    nativePort.onDisconnect.addListener(() => {
                        let errMsg = chrome.runtime.lastError ? chrome.runtime.lastError.message : "Disconnected from Host";
                        port.postMessage({ status: "error", message: errMsg });
                    });
                    
                    // Obtener cookies de kemono.cr y kemono.su para pasarlas a Python
                    chrome.cookies.getAll({ domain: "kemono.cr" }, (cookiesCR) => {
                        chrome.cookies.getAll({ domain: "kemono.su" }, (cookiesSU) => {
                            let allCookies = [...cookiesCR, ...cookiesSU];
                            let cookieStr = allCookies.map(c => c.name + "=" + c.value).join("; ");
                            
                            // User Agent del navegador actual
                            let ua = navigator.userAgent;
                            
                            msg.cookies = cookieStr;
                            msg.userAgent = ua;
                            
                            nativePort.postMessage(msg);
                        });
                    });
                } catch(e) {
                    port.postMessage({ status: "error", message: e.message });
                }
            }
        });

        port.onDisconnect.addListener(() => {
            if (nativePort) nativePort.disconnect();
        });
    }
});