Created
May 16, 2025 05:22
-
-
Save betillogalvanfbc/c3b130fd330253d2096d33086ceaa6ea to your computer and use it in GitHub Desktop.
extracturlsdomains.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(() => { | |
/* ---------- 1. Recolectar candidatos ---------- */ | |
const nodes = Array.from(document.querySelectorAll('*')); | |
/* A) Atributos típicos con links */ | |
const attrNames = ['href', 'src', 'action', 'data', 'poster']; | |
const attrUrls = nodes.flatMap(node => | |
attrNames | |
.map(a => node.getAttribute?.(a)) | |
.filter(Boolean) | |
); | |
/* B) URLs en CSS: url("...") url('...') url(...) */ | |
const cssUrls = Array.from(document.styleSheets || []).flatMap(sheet => { | |
try { | |
return Array.from(sheet.cssRules || []).flatMap(rule => { | |
const text = rule.cssText || ''; | |
return [...text.matchAll(/url\((['"]?)(.*?)\1\)/gi)].map(m => m[2]); | |
}); | |
} catch { | |
/* CORS: hojas externas — las ignoramos */ | |
return []; | |
} | |
}); | |
/* C) URLs en texto plano o JS inline */ | |
const textNodes = []; | |
const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); | |
for (let n; (n = walker.nextNode()); ) textNodes.push(n.nodeValue); | |
const textUrls = textNodes.flatMap(t => | |
[...t.matchAll(/\bhttps?:\/\/[^\s"')]+/gi)].map(m => m[0]) | |
); | |
/* ---------- 2. Normalizar y filtrar ---------- */ | |
const toAbsolute = url => new URL(url, location.href).href; // resuelve relativas | |
const raw = [...attrUrls, ...cssUrls, ...textUrls]; | |
const absUrls = [...new Set( | |
raw | |
.filter(u => /^https?:\/\//i.test(u) || /^[/.]/.test(u)) // absolutas o relativas | |
.map(toAbsolute) | |
)]; | |
/* ---------- 3. Dominios ---------- */ | |
const domains = [...new Set(absUrls.map(u => { | |
try { | |
const { hostname, port } = new URL(u); | |
return port ? `${hostname}:${port}` : hostname; | |
} catch { | |
return null; | |
} | |
}).filter(Boolean))]; | |
/* ---------- 4. Mostrar y copiar ---------- */ | |
console.log('🔗 URLs encontradas (' + absUrls.length + '):', absUrls); | |
console.log('🌐 Dominios únicos (' + domains.length + '):', domains); | |
const clipboardText = absUrls.join('\n'); | |
/* DevTools de Chromium exponen copy(); otros navegadores, fallback a Clipboard API */ | |
try { | |
copy(clipboardText); // Chrome, Edge, Brave… | |
console.log('✅ Copiadas al portapapeles'); | |
} catch { | |
navigator.clipboard.writeText(clipboardText) | |
.then(() => console.log('✅ Copiadas al portapapeles')) | |
.catch(err => console.warn('❌ No se pudo copiar:', err)); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment