summaryrefslogtreecommitdiff
path: root/sysret.org/themes/tabi-lean/static/js/decodeMail.js
blob: f26ac68699b7dc8b99531bcaaf30716eed715f8c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
(function () {
    'use strict';

    // Utility function: Base64 Decoding.
    function decodeBase64(encodedString) {
        try {
            // Can't use atob() directly because it doesn't support non-ascii characters.
            // And non-ascii characters are allowed in email addresses and domains.
            // See https://en.wikipedia.org/wiki/Email_address#Internationalization
            // Code below adapted from Jackie Han: https://stackoverflow.com/a/64752311
            const byteString = atob(encodedString);

            // Convert byteString to an array of char codes.
            const charCodes = [...byteString].map((char) => char.charCodeAt(0));

            // Use TypedArray.prototype.set() to copy the char codes into a Uint8Array.
            const bytes = new Uint8Array(charCodes.length);
            bytes.set(charCodes);

            const decoder = new TextDecoder('utf-8');
            return decoder.decode(bytes);
        } catch (e) {
            console.error('Failed to decode Base64 string: ', e);
            return null;
        }
    }

    // Utility function: Update href of an element with a decoded email.
    function updateEmailHref(element) {
        const encodedEmail = element.getAttribute('data-encoded-email');
        const decodedEmail = decodeBase64(encodedEmail);

        if (decodedEmail) {
            element.setAttribute('href', `mailto:${decodedEmail}`);
        } else {
            // If the decoding fails, hide the email link.
            element.style.display = 'none';
        }
    }

    // Fetch and process email elements with the "data-encoded-email" attribute.
    const encodedEmailElements = document.querySelectorAll('[data-encoded-email]');
    encodedEmailElements.forEach(updateEmailHref);
})();