Last active
November 20, 2023 18:11
-
-
Save adactio/8be51468ff8c9591f9c98e0612bfae16 to your computer and use it in GitHub Desktop.
Intercept clicks on external links and pass them to a redirector.
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
document.body.addEventListener('click', function(event) { | |
var target = event.target.closest('a[href]'); | |
if (!target) return; | |
var href = encodeURIComponent(target.getAttribute('href')); | |
if (!href.startsWith('http')) return | |
if (href.includes('archive.org')) return | |
var url = `/redirect.php?url=${href}`; | |
if (target.closest('.h-entry') && target.closest('.h-entry').querySelector('time.dt-published[datetime]')) { | |
var date = encodeURIComponent(target.closest('.h-entry').querySelector('time.dt-published').getAttribute('datetime')); | |
url += `&date=${date}`; | |
} | |
target.href = url; | |
}, true); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
In this case, the links are being passed to a PHP file called redirect.php:
https://gist.github.com/adactio/3d6983bea9b30c993a65b12537ce930c