-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
27 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,27 @@ | ||
// Open links externally. | ||
var links = document.links; | ||
|
||
for (var i = 0, linksLength = links.length; i < linksLength; i++) { | ||
if (links[i].hostname != window.location.hostname) { | ||
links[i].target = '_blank'; | ||
} | ||
} | ||
// Funzione per aprire in una nuova scheda tutti i link esterni e i link ai file PDF | ||
function targetBlankForExternalAndPDFs() { | ||
// Ottieni l'host del sito corrente senza sottodomini | ||
var internal = location.hostname.replace("www.", ""); | ||
var internalRegex = new RegExp(internal, "i"); | ||
|
||
// Aggiungi un'espressione regolare per riconoscere anche gli indirizzi IP locali | ||
var localHost = /localhost|127\.0\.0\.1/; | ||
|
||
// Prendi tutti i link presenti nella pagina | ||
var links = document.getElementsByTagName('a'); | ||
for (var i = 0; i < links.length; i++) { | ||
var href = links[i].href; // Ottieni l'URL di ogni link | ||
|
||
// Controlla se l'URL è considerato interno o esterno, e se punta a un file PDF | ||
if ((internalRegex.test(links[i].hostname) || localHost.test(links[i].hostname)) && href.endsWith('.pdf')) { | ||
// Se il link è interno (incluso localhost o IP locale) e punta a un PDF, apri in una nuova scheda | ||
links[i].setAttribute('target', '_blank'); | ||
} else if (!internalRegex.test(links[i].hostname) && !localHost.test(links[i].hostname) || href.endsWith('.pdf')) { | ||
// Se il link è esterno o punta a un PDF, apri in una nuova scheda | ||
links[i].setAttribute('target', '_blank'); | ||
} | ||
} | ||
}; | ||
|
||
// Esegui la funzione per applicare la regola ai link | ||
targetBlankForExternalAndPDFs(); |