hugo-theme/layouts/shortcodes/protected-email.html

39 lines
1.3 KiB
HTML

{{ $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;">
{{ T "email_protection" }}
</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 }}