Skip to content

Instantly share code, notes, and snippets.

@rockthrower69
Last active Nov 15, 2020
Embed
What would you like to do?
'use strict';
! function () {
function logerr(msg) {
try {
if("undefined" == typeof console) {
return;
}
if("error" in console) {
console.error(msg);
} else {
console.log(msg);
}
} catch (err) {}
}
function createLink(mail) {
return tempDiv.innerHTML = '<a href="' + mail.replace(/"/g, "&quot;") + '"></a>', tempDiv.childNodes[0].getAttribute("href") || "";
}
function parseHex(string, position) {
var result = string.substr(position, 2);
return parseInt(result, 16);
}
function decode(href, startPos) {
var result = "";
var key = parseHex(href, startPos);
for(var position = startPos + 2; position < href.length; position = position + 2) {
var byte = parseHex(href, position) ^ key;
result = result + String.fromCharCode(byte);
}
try {
result = decodeURIComponent(escape(result));
} catch (err) {
logerr(err);
}
return createLink(result);
}
function searchLinks(doc) {
var anchors = doc.querySelectorAll("a");
for(var i = 0; i < anchors.length; i++) {
try {
var anchor = anchors[i];
var isCF = anchor.href.indexOf(linkSelector);
if(isCF > -1) {
anchor.href = "mailto:" + decode(anchor.href, isCF + linkSelector.length);
}
} catch (err) {
logerr(err);
}
}
}
function searchClasses(doc) {
var elements = doc.querySelectorAll(classSelector);
for(var i = 0; i < elements.length; i++) {
try {
var element = elements[i];
var parent = element.parentNode;
var code = element.getAttribute(dataSelector);
if(code) {
var mail = decode(code, 0);
var txtElement = document.createTextNode(mail);
parent.replaceChild(txtElement, element);
}
} catch (err) {
logerr(err);
}
}
}
function searchTemplates(doc) {
var templates = doc.querySelectorAll("template");
for(var i = 0; i < templates.length; i++) {
try {
init(templates[i].content);
} catch (err) {
logerr(err);
}
}
}
function init(doc) {
try {
searchLinks(doc);
searchClasses(doc);
searchTemplates(doc);
} catch (err) {
logerr(err);
}
}
var linkSelector = "/cdn-cgi/l/email-protection#";
var classSelector = ".__cf_email__";
var dataSelector = "data-cfemail";
var tempDiv = document.createElement("div");
init(document);
(function () {
var curScript = document.currentScript || document.scripts[document.scripts.length - 1];
curScript.parentNode.removeChild(curScript);
})();
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment