-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.ts
116 lines (105 loc) · 4.33 KB
/
script.ts
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
(() => {
const duckMailerForm = document.getElementById("duckmailer-form");
const outputContainer = document.getElementById("output-container");
const output = document.getElementById("output-email");
const outputCopyLink: HTMLButtonElement | null = document.querySelector(
"#output-email-copy-link"
);
const duckErrorMsg = document.getElementById("duck-error-msg");
const targetErrorMsg = document.getElementById("target-error-msg");
const snackbar = document.getElementById("snackbar");
let duckAddress: HTMLInputElement | null;
let targetAddress: HTMLInputElement | null;
let convertButton: HTMLButtonElement | null;
if (duckMailerForm) {
duckAddress = duckMailerForm.querySelector('input[name="duck-email"]');
targetAddress = duckMailerForm.querySelector('input[name="target-email"]');
convertButton = duckMailerForm.querySelector('button[type="submit"]');
duckMailerForm.addEventListener("submit", (e) => {
e.preventDefault();
const duck = duckAddress?.value;
const target = targetAddress?.value;
outputContainer && (outputContainer.style.display = "none");
output && (output.textContent = "");
duckErrorMsg && (duckErrorMsg.style.display = "none");
targetErrorMsg && (targetErrorMsg.style.display = "none");
if (duck && !validDuckEmail(duck)) {
duckErrorMsg && (duckErrorMsg.style.display = "block");
duckErrorMsg &&
(duckErrorMsg.innerHTML =
"Please enter a valid @duck.com email address");
return;
} else {
duckErrorMsg && (duckErrorMsg.style.display = "none");
}
if (target && !validEmail(target)) {
targetErrorMsg && (targetErrorMsg.style.display = "block");
targetErrorMsg &&
(targetErrorMsg.innerHTML = "Please enter a valid email address");
return;
} else {
targetErrorMsg && (targetErrorMsg.style.display = "none");
}
const convertedEmail = duck && target && convertEmail(duck, target);
outputContainer && (outputContainer.style.display = "block");
output && (output.innerText = convertedEmail || "");
// copy to clipboard
copyToClipboard(convertedEmail || "");
});
}
outputCopyLink?.addEventListener("click", () => {
copyToClipboard(output?.innerText.trim() || "");
});
// validate email address
const validEmail = (email: string) => {
const re =
/^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
return re.test(email.toLowerCase());
};
// validate @duck.com email address
const validDuckEmail = (email: string) => {
const re = /^([a-zA-Z0-9_\-\.]+)@duck.com$/;
return re.test(email.toLowerCase());
};
// convert email address
const convertEmail = (duck: string, target: string): string => {
const duckParts = duck.split("@");
const targetParts = target.split("@");
const duckName = duckParts[0];
const duckDomain = duckParts[1];
const targetName = targetParts[0];
const targetDomain = targetParts[1];
// duck address: [email protected]
// target address: [email protected]
// converted address: [email protected]
return `${targetName}_at_${targetDomain}_${duckName}@${duckDomain}`;
};
const copyToClipboard = (copy: string) => {
outputCopyLink && (outputCopyLink.innerText = "Copy");
navigator.clipboard
.writeText(copy)
.then(() => {
showSnackbar("Email copied to clipboard");
outputCopyLink && (outputCopyLink.innerText = "Copied");
// disable button
convertButton && (convertButton.disabled = true);
outputCopyLink && (outputCopyLink.disabled = true);
setTimeout(() => {
outputCopyLink && (outputCopyLink.innerText = "Copy");
convertButton && (convertButton.disabled = false);
outputCopyLink && (outputCopyLink.disabled = false);
}, 3500);
})
.catch((err) => {
showSnackbar("Error copying email to clipboard");
});
};
function showSnackbar(msg: string) {
snackbar && (snackbar.innerText = msg);
snackbar && snackbar.classList.add("show");
outputCopyLink && (outputCopyLink.innerText = "Copied");
setTimeout(() => {
snackbar && snackbar.classList.remove("show");
}, 3000);
}
})();