-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
73 lines (59 loc) · 2.01 KB
/
index.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
function copyTextToClipboard(text) {
// shoutout to http://stackoverflow.com/a/30810322 for the copy code below
var textArea = document.createElement("textarea");
textArea.style.position = 'fixed';
textArea.style.top = 0;
textArea.style.left = 0;
textArea.style.width = '2em';
textArea.style.height = '2em';
textArea.style.padding = 0;
textArea.style.border = 'none';
textArea.style.outline = 'none';
textArea.style.boxShadow = 'none';
textArea.style.background = 'transparent';
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
try {
var successful = document.execCommand('copy');
var msg = successful ? 'successful' : 'unsuccessful';
console.log('Copying text command was ' + msg);
} catch (err) {
console.log('Oops, unable to copy');
}
document.body.removeChild(textArea);
}
const findMailto = (element) => {
if (element.matches("a[href^='mailto']")){
return element
}else if (element.nodeName == 'BODY'){
return false
}else{
return findMailto(element.parentElement)
}
}
const addTooltip = (linkElement, emailAddress) => {
const newSpan = `<span class='emailto-clipboard-tooltiptext'>${emailAddress} copied to clipboard!</span>`;
const originalClassName = linkElement.className;
const originalInnderHTML = linkElement.innerHTML;
linkElement.className = originalClassName + " emailto-clipboard-tooltip";
linkElement.innerHTML = originalInnderHTML + newSpan;
setTimeout(()=>{
linkElement.innerHTML = originalInnderHTML;
linkElement.className = originalClassName;
}, 4000)
}
const listenForMailtos = () => {
const bodyElement = document.querySelector('body');
bodyElement.onclick = (event) => {
const initialElement = event.target
const mailtoLink = findMailto(initialElement);
if (mailtoLink){
event.preventDefault();
const emailAddress = mailtoLink.href.slice(7).split("?")[0];
addTooltip(mailtoLink, emailAddress);
copyTextToClipboard(emailAddress);
}
}
}
listenForMailtos();