document.addEventListener('DOMContentLoaded', () => {
const host = window.location.hostname;
document.querySelectorAll('a[href]').forEach(link => {
let url;
try {
url = new URL(link.href, window.location.href);
} catch {
return; // ignora hrefs inválidos (ex: "javascript:", "#", "mailto:")
}
// só age em links http/https que apontam para outro domínio
const isExterno =
(url.protocol === 'http:' || url.protocol === 'https:') &&
url.hostname !== host;
if (isExterno) {
link.setAttribute('target', '_self');
// remove rel="noopener" que só faz sentido com _blank
link.removeAttribute('rel');
}
});
});