-
Notifications
You must be signed in to change notification settings - Fork 1
/
safe-email.js
54 lines (53 loc) · 1.89 KB
/
safe-email.js
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
45
46
47
48
49
50
51
52
53
54
/* eslint-disable no-param-reassign */
(() => {
const dataAttrNames = {
email: 'email_b64',
body: 'body',
subject: 'subject',
};
const defaults = {
body: 'Hi!',
subject: getEmailSubject(),
};
function getEmailSubject() {
const currentLocation = window.location.hostname + window.location.pathname;
return currentLocation.endsWith('/') ? currentLocation.slice(0, -1) : currentLocation;
}
function obfuscateInnerHtml(text) {
const chars = text.split('');
const reversed = chars.reverse();
let result = '';
reversed.forEach((char) => {
const randomText = Math.random().toString(36).substring(7);
result += `<em style="display:none;">${randomText}</em>`;
result += `<span>${char}</span>`;
});
return result;
}
function styleElement(element) {
element.style.direction = 'rtl';
element.style['unicode-bidi'] = 'bidi-override';
}
function initializeElement(element) {
const readAttr = (name) => element.dataset[name];
styleElement(element);
if (!element.innerHTML.trim()) {
element.innerHTML = obfuscateInnerHtml(atob(readAttr(dataAttrNames.email)));
}
element.addEventListener('click', (ev) => {
const href = atob('bWFpbHRvOg==') /* mailto: */ + atob(readAttr(dataAttrNames.email))
+ atob('P3N1YmplY3Q9') /* ?subject= */ + (readAttr(dataAttrNames.subject) || defaults.subject)
+ atob('JmJvZHk9') /* &body= */ + (readAttr(dataAttrNames.body) || defaults.body);
ev.preventDefault();
window.location.href = href;
});
}
function initializeAll() {
const elements = document.querySelectorAll(`[data-${dataAttrNames.email}]`);
elements.forEach((element) => initializeElement(element));
}
if (!window.areEmailsInitialized) {
window.areEmailsInitialized = true;
document.addEventListener('DOMContentLoaded', () => initializeAll());
}
})();