detector_de_enlaces / background.js
codebybrody's picture
Upload 10 files
e9264a7 verified
Raw History Blame Contribute Delete
4.54 kB
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();
});
}
});