puggan icon

cf-email-decode.js

puggan | PRO | 10/02/19 03:29:31 PM UTC | 0 ⭐ | 649 👁️ | Never ⏰ | []
JavaScript |

3.76 KB

|

None

|

0 👍

/

0 👎

!(() => {
    "use strict";
 
    // Error handler
    function handleError(errorMessage) {
        try {
            if ("undefined" == typeof console) {
                return;
            }
            "error" in console ? console.error(errorMessage) : console.log(errorMessage);
        } catch (e) {
        }
    }
 
    // URL Decode?
    function urlDecode(url) {
        divElement.innerHTML = '<a href="' + url.replace(/"/g, "&quot;") + '"></a>';
        return divElement.childNodes[0].getAttribute("href") || "";
    }
 
    // convert a hex par at position t in the string e
    function decodeHexPair(hexString, offset) {
        const pairString = hexString.substr(offset, 2);
        return parseInt(pairString, 16);
    }
 
    function unmaskUrl(hexString, offset) {
        let url = "";
        const xorMask = decodeHexPair(hexString, offset);
 
        for (let index = offset + 2; index < hexString.length; index += 2) {
            let ascciNumber = decodeHexPair(hexString, index) ^ xorMask;
            url += String.fromCharCode(ascciNumber);
        }
 
        try {
            url = decodeURIComponent(escape(url));
        } catch (errorMessage) {
            handleError(errorMessage);
        }
        return urlDecode(url);
    }
 
    function fixLink(baseElement) {
        const linkList = baseElement.querySelectorAll("a");
        for (let index = 0; index < linkList.length; index++) {
            try {
                const link = linkList[index];
                const offset = link.href.indexOf(emailProtectionPrefix);
                if (offset > -1) {
                    link.href = "mailto:" + unmaskUrl(link.href, offset + emailProtectionPrefix.length);
                }
            } catch (errorMessage) {
                handleError(errorMessage)
            }
        }
    }
 
    function restorePlainTextEmails(baseElement) {
        const protectedElementsList = baseElement.querySelectorAll(emailProtectionClass);
        for (let index = 0; index < protectedElementsList.length; index++) {
            try {
                const protectedElement = protectedElementsList[index];
                const protectedElementParent = protectedElement.parentNode;
                const encryptedEmail = protectedElement.getAttribute(emailProtectionAttribute);
                if (encryptedEmail) {
                    var emailAdress = unmaskUrl(encryptedEmail, 0);
                    var emailAdressElement = document.createTextNode(emailAdress);
                    protectedElementParent.replaceChild(emailAdressElement, protectedElement);
                }
            } catch (h) {
                handleError(h)
            }
        }
    }
 
    function a(baseElement) {
        const templateList = baseElement.querySelectorAll("template");
        for (let index = 0; index < templateList.length; index++) {
            try {
                init(templateList[index].content)
            } catch (errorMessage) {
                handleError(errorMessage)
            }
        }
    }
 
    function init(baseElement) {
        try {
            fixLink(baseElement);
            restorePlainTextEmails(baseElement);
            a(baseElement);
        } catch (errorMessage) {
            handleError(errorMessage)
        }
    }
 
    const emailProtectionPrefix = "/cdn-cgi/l/email-protection#";
    const emailProtectionClass = ".__cf_email__";
    const emailProtectionAttribute = "data-cfemail";
    const divElement = document.createElement("div");
 
    init(document);
    (() => {
        const currentScriptElement = document.currentScript || document.scripts[document.scripts.length - 1];
        currentScriptElement.parentNode.removeChild(currentScriptElement)
    })();
})();

Comments