feat: added email protection

This commit is contained in:
Niklas 2024-10-22 00:36:13 +02:00
parent a117e97a2f
commit ceb6263204
2 changed files with 41 additions and 0 deletions

View File

@ -24,3 +24,5 @@ Hugo is for people who want to hand code their own website without worrying abou
Websites built with Hugo are extremely fast, secure and can be deployed anywhere including, AWS, GitHub Pages, Heroku, Netlify and any other hosting provider. Websites built with Hugo are extremely fast, secure and can be deployed anywhere including, AWS, GitHub Pages, Heroku, Netlify and any other hosting provider.
Learn more and contribute on [GitHub](https://github.com/gohugoio). Learn more and contribute on [GitHub](https://github.com/gohugoio).
{{< protected-email "admin@example.com" >}}

View File

@ -0,0 +1,39 @@
{{ $email := .Get 0 }}
{{ $parts := split $email "@" }}
{{ $name := index $parts 0 | base64Encode }}
{{ $domainParts := split (index $parts 1) "." }}
{{ $domain := delimit (first (sub (len $domainParts) 1) $domainParts) "." | base64Encode }}
{{ $tld := index $domainParts (sub (len $domainParts) 1) | base64Encode }}
<a href="#" class="protected-email"
data-name="{{ $name }}"
data-domain="{{ $domain }}"
data-tld="{{ $tld }}"
onclick="decodeEmail(this);return false;">
Click to reveal email
</a>
{{ if not (.Page.Scratch.Get "protectedEmailJS") }}
{{ .Page.Scratch.Set "protectedEmailJS" true }}
<script>
function decodeEmail(element) {
const name = atob(element.getAttribute('data-name'));
const domain = atob(element.getAttribute('data-domain'));
const tld = atob(element.getAttribute('data-tld'));
const email = name + '@' + domain + '.' + tld;
element.href = 'mailto:' + email;
element.textContent = email;
// Remove the onclick attribute to prevent further obfuscation
element.onclick = null;
}
// Initialize all protected email links
document.addEventListener('DOMContentLoaded', function() {
document.querySelectorAll('.protected-email').forEach(function(link) {
link.style.cursor = 'pointer';
});
});
</script>
{{ end }}