-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontent.js
More file actions
132 lines (113 loc) · 3.7 KB
/
Copy pathcontent.js
File metadata and controls
132 lines (113 loc) · 3.7 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
(function () {
const TOAST_ID = "__mailto_copy_hijacker_toast__";
function extractEmail(rawHref) {
if (!rawHref || !rawHref.toLowerCase().startsWith("mailto:")) {
return null;
}
const withoutScheme = rawHref.slice(7);
const queryIndex = withoutScheme.indexOf("?");
const addressPart = queryIndex >= 0 ? withoutScheme.slice(0, queryIndex) : withoutScheme;
const decoded = decodeURIComponent(addressPart).trim();
return decoded || null;
}
async function copyText(text) {
try {
await navigator.clipboard.writeText(text);
return true;
} catch (error) {
const textarea = document.createElement("textarea");
textarea.value = text;
textarea.setAttribute("readonly", "true");
textarea.style.position = "fixed";
textarea.style.top = "-9999px";
textarea.style.left = "-9999px";
document.documentElement.appendChild(textarea);
textarea.select();
try {
return document.execCommand("copy");
} finally {
textarea.remove();
}
}
}
function showToast(message, isError) {
const doc = document;
if (!doc || !doc.body) {
window.setTimeout(() => showToast(message, isError), 50);
return;
}
let toast = doc.getElementById(TOAST_ID);
if (!toast) {
toast = doc.createElement("div");
toast.id = TOAST_ID;
toast.style.position = "fixed";
toast.style.right = "16px";
toast.style.bottom = "16px";
toast.style.zIndex = "2147483647";
toast.style.maxWidth = "320px";
toast.style.padding = "12px 14px";
toast.style.borderRadius = "10px";
toast.style.boxShadow = "0 10px 30px rgba(0, 0, 0, 0.2)";
toast.style.fontFamily = "ui-sans-serif, system-ui, sans-serif";
toast.style.fontSize = "14px";
toast.style.lineHeight = "1.4";
toast.style.color = "#ffffff";
toast.style.opacity = "0";
toast.style.transform = "translateY(12px)";
toast.style.transition = "opacity 140ms ease, transform 140ms ease";
toast.style.pointerEvents = "none";
doc.body.appendChild(toast);
}
toast.textContent = message;
toast.style.background = isError ? "#b42318" : "#111827";
toast.style.opacity = "1";
toast.style.transform = "translateY(0)";
window.clearTimeout(showToast.dismissTimer);
showToast.dismissTimer = window.setTimeout(() => {
toast.style.opacity = "0";
toast.style.transform = "translateY(12px)";
}, 1800);
}
async function handleMailtoLink(link, event) {
const href = link.getAttribute("href") || link.href;
const email = extractEmail(href);
if (!email) {
return;
}
event.preventDefault();
event.stopPropagation();
event.stopImmediatePropagation();
const copied = await copyText(email);
if (copied) {
showToast(`Copied email: ${email}`, false);
} else {
showToast("Could not copy email address", true);
}
}
function getMailtoAnchor(target) {
if (!(target instanceof Element)) {
return null;
}
return target.closest('a[href^="mailto:"], a[href^="MAILTO:"]');
}
function onPointerActivation(event) {
const link = getMailtoAnchor(event.target);
if (!link) {
return;
}
void handleMailtoLink(link, event);
}
function onKeyboardActivation(event) {
if (event.key !== "Enter" && event.key !== " ") {
return;
}
const link = getMailtoAnchor(event.target);
if (!link) {
return;
}
void handleMailtoLink(link, event);
}
document.addEventListener("click", onPointerActivation, true);
document.addEventListener("auxclick", onPointerActivation, true);
document.addEventListener("keydown", onKeyboardActivation, true);
})();