(() => { if (window.__epornerSelectorActive) return; window.__epornerSelectorActive = true; const selectedLinks = new Set(); const container = document.createElement('div'); container.id = 'eporner-floating-ui'; container.style.cssText = ` position: fixed; bottom: 20px; right: 20px; background: #18181b; color: #fff; padding: 15px; border-radius: 10px; border: 1px solid #27272a; z-index: 999999; font-family: sans-serif; box-shadow: 0 4px 12px rgba(0,0,0,0.5); display: flex; flex-direction: column; gap: 10px; min-width: 200px; `; const countDisplay = document.createElement('div'); countDisplay.textContent = '0 enlaces seleccionados'; countDisplay.style.fontWeight = 'bold'; countDisplay.style.textAlign = 'center'; countDisplay.style.fontSize = '14px'; const selectAllBtn = document.createElement('button'); selectAllBtn.textContent = 'Seleccionar Todos'; selectAllBtn.style.cssText = ` background: #3f3f46; color: white; border: none; padding: 8px; border-radius: 6px; cursor: pointer; font-weight: 600; font-size: 13px; transition: background 0.2s; `; selectAllBtn.onmouseover = () => selectAllBtn.style.background = '#52525b'; selectAllBtn.onmouseout = () => selectAllBtn.style.background = '#3f3f46'; const copyBtn = document.createElement('button'); copyBtn.textContent = 'Copiar Enlaces'; copyBtn.style.cssText = ` background: #ff5500; color: white; border: none; padding: 10px; border-radius: 6px; cursor: pointer; font-weight: bold; `; const closeBtn = document.createElement('button'); closeBtn.id = 'eporner-close-btn'; closeBtn.textContent = 'Cerrar Selector'; closeBtn.style.cssText = ` background: #27272a; color: white; border: 1px solid #3f3f46; padding: 8px; border-radius: 6px; cursor: pointer; font-size: 13px; `; container.appendChild(countDisplay); container.appendChild(selectAllBtn); container.appendChild(copyBtn); container.appendChild(closeBtn); document.body.appendChild(container); const style = document.createElement('style'); style.id = 'eporner-style'; style.textContent = ` .eporner-selector-selected { position: relative !important; outline: 4px solid #10b981 !important; outline-offset: -4px !important; } .eporner-selector-selected::after { content: '✓' !important; position: absolute !important; top: 10px !important; left: 10px !important; background: #10b981 !important; color: white !important; border-radius: 50% !important; width: 28px !important; height: 28px !important; display: flex !important; align-items: center !important; justify-content: center !important; font-weight: bold !important; font-size: 16px !important; z-index: 2147483647 !important; box-shadow: 0 2px 5px rgba(0,0,0,0.5) !important; pointer-events: none !important; } `; document.head.appendChild(style); // Helper to get normalized URLs for matching const getCleanUrl = (href) => { if (!href) return ''; let url = href.split('?')[0].split('#')[0].trim(); if (url.endsWith('/')) { url = url.slice(0, -1); } return url; }; // Helper to check if an anchor is a thumbnail cover rather than text title (robust and crash-proof) const isThumbnailAnchor = (a) => { if (!a) return false; if (a.__isThumbnail) return true; // 1. If it contains an image, video, canvas, or thumbnail class-like elements if (a.querySelector('img, picture, canvas, video, [class*="thumb"], [class*="img"], [class*="preview"], [class*="cover"]')) return true; // 2. Safe class name parsing (prevent crashes on animated SVG string wrappers) let className = ''; if (typeof a.className === 'string') { className = a.className.toLowerCase(); } else if (a.className && typeof a.className.baseVal === 'string') { className = a.className.baseVal.toLowerCase(); } if (className.includes('thumb') || className.includes('img') || className.includes('preview') || className.includes('cover') || className.includes('vimg')) return true; // 3. Fallback: if it has short text content and wraps some tag, it is likely a thumbnail (like Eporner lazy loaders) const textLen = a.textContent.trim().length; if (textLen < 15 && a.querySelector('*')) return true; return false; }; // Capture and save initial attributes before they are mutated by site scripts const initAnchors = () => { document.querySelectorAll('a[href*="/video-"]').forEach(a => { if (isThumbnailAnchor(a)) { a.__isThumbnail = true; if (!a.__originalVideoUrl) { const url = getCleanUrl(a.href); if (url && !url.startsWith('javascript') && url !== '#' && !url.includes('void(0)')) { a.__originalVideoUrl = url; } } } }); }; // Run initial scan initAnchors(); // Scan periodically to catch new elements (scroll/pagination) const initInterval = setInterval(initAnchors, 800); const toggleCardSelection = (url, select) => { initAnchors(); document.querySelectorAll('a[href*="/video-"]').forEach(a => { if (a.__originalVideoUrl === url && a.__isThumbnail) { if (select) { a.classList.add('eporner-selector-selected'); } else { a.classList.remove('eporner-selector-selected'); } } }); }; const clickHandler = (e) => { const card = e.target.closest('a[href*="/video-"]'); if (card) { initAnchors(); if (card.__isThumbnail) { let url = card.__originalVideoUrl; if (!url && card.href && !card.href.startsWith('javascript') && card.href !== '#') { url = getCleanUrl(card.href); card.__originalVideoUrl = url; } if (!url) return; e.preventDefault(); e.stopPropagation(); if (selectedLinks.has(url)) { selectedLinks.delete(url); toggleCardSelection(url, false); } else { selectedLinks.add(url); toggleCardSelection(url, true); } countDisplay.textContent = selectedLinks.size + ' enlaces seleccionados'; } } }; document.addEventListener('click', clickHandler, true); selectAllBtn.onclick = () => { initAnchors(); const links = []; document.querySelectorAll('a[href*="/video-"]').forEach(a => { if (a.__isThumbnail && a.__originalVideoUrl) { links.push(a); } }); let allSelected = true; links.forEach(a => { const url = a.__originalVideoUrl; if (!selectedLinks.has(url)) { allSelected = false; } }); if (allSelected && selectedLinks.size > 0) { links.forEach(a => { const url = a.__originalVideoUrl; selectedLinks.delete(url); toggleCardSelection(url, false); }); selectAllBtn.textContent = 'Seleccionar Todos'; } else { links.forEach(a => { const url = a.__originalVideoUrl; if (!selectedLinks.has(url)) { selectedLinks.add(url); toggleCardSelection(url, true); } }); selectAllBtn.textContent = 'Deseleccionar Todos'; } countDisplay.textContent = selectedLinks.size + ' enlaces seleccionados'; }; copyBtn.onclick = () => { if (selectedLinks.size === 0) return; const text = Array.from(selectedLinks).join('\n'); navigator.clipboard.writeText(text).then(() => { const ogText = copyBtn.textContent; copyBtn.textContent = '¡Copiados!'; copyBtn.style.background = '#10b981'; setTimeout(() => { copyBtn.textContent = ogText; copyBtn.style.background = '#ff5500'; }, 2000); }); }; closeBtn.onclick = () => { clearInterval(initInterval); document.removeEventListener('click', clickHandler, true); container.remove(); style.remove(); document.querySelectorAll('.eporner-selector-selected').forEach(el => el.classList.remove('eporner-selector-selected')); window.__epornerSelectorActive = false; try { chrome.runtime.sendMessage({ type: 'UI_CLOSED', tool: 'eporner' }); } catch (e) { console.log("Could not notify popup, it is probably closed"); } window.dispatchEvent(new Event('EPORNER_OFF')); }; })();