-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
349 lines (285 loc) · 12.5 KB
/
Copy pathscript.js
File metadata and controls
349 lines (285 loc) · 12.5 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
let account = null;
let inboxInterval = null;
const API_BASE = "/api/tempmail";
async function apiRequest(path, options = {}) {
const response = await fetch(`${API_BASE}${path}`, {
credentials: "same-origin",
...options,
headers: {
"Content-Type": "application/json",
...(options.headers || {}),
},
});
let data = null;
try {
data = await response.json();
} catch {
data = null;
}
return { response, data };
}
function resetSessionState(clearEmailDisplay = true) {
account = null;
localStorage.removeItem("tm_email");
localStorage.removeItem("tm_read_messages");
if (inboxInterval) {
clearInterval(inboxInterval);
inboxInterval = null;
}
if (clearEmailDisplay) {
const emailDisplay = document.getElementById("emailDisplay");
if (emailDisplay) emailDisplay.innerText = "---";
}
}
async function generateAccount() {
try {
const username = Math.random().toString(36).substring(2, 10);
// Get domains through serverless proxy
const domainsResult = await apiRequest("/domains", { method: "GET" });
if (!domainsResult.response.ok) { showAlert("Failed to fetch email domains. Please try again."); return; }
const domainData = domainsResult.data;
if (!domainData["hydra:member"] || domainData["hydra:member"].length === 0) {
showAlert("No email domains available. Please try again later."); return;
}
const domain = domainData["hydra:member"][0].domain;
const address = `${username}@${domain}`;
const password = Math.random().toString(36).substring(2, 12);
// Create account through serverless proxy
const accountResult = await apiRequest("/accounts", {
method: "POST",
body: JSON.stringify({ address, password }),
});
if (!accountResult.response.ok) { showAlert("Failed to create temp email. Please try again."); return; }
account = { address };
const emailDisplay = document.getElementById("emailDisplay");
if (emailDisplay) emailDisplay.innerText = address;
localStorage.setItem("tm_email", address);
// Create secure server session (token is stored in HttpOnly cookie by function)
const sessionResult = await apiRequest("/session", {
method: "POST",
body: JSON.stringify({ address, password }),
});
if (!sessionResult.response.ok) {
showAlert("Account created but failed to start secure session. Please try again.");
return;
}
// Start polling inbox
if (inboxInterval) clearInterval(inboxInterval);
checkInbox();
inboxInterval = setInterval(checkInbox, 15000);
showAlert("Email account created successfully!");
} catch (error) {
showAlert("An error occurred. Please try again.");
console.error(error);
}
}
function showAlert(message) {
const alert = document.getElementById("alert");
const alertMessage = document.getElementById("alertMessage");
if (alert && alertMessage) {
alertMessage.textContent = message;
alert.classList.add("show");
setTimeout(() => { closeAlert(); }, 3000);
} else {
console.log("Alert:", message);
}
}
function closeAlert() {
const alert = document.getElementById("alert");
if (alert) alert.classList.remove("show");
}
function copyEmail() {
const email = document.getElementById("emailDisplay")?.innerText;
if (!email || email === "---") { showAlert("Please generate an email first!"); return; }
const copyIcons = document.querySelectorAll(".fa-copy");
copyIcons.forEach((icon) => {
icon.classList.remove("icon-animate-copy");
void icon.offsetWidth;
icon.classList.add("icon-animate-copy");
});
navigator.clipboard.writeText(email)
.then(() => showAlert("Email copied to clipboard!"))
.catch(() => showAlert("Failed to copy email"));
}
// Email & inbox spin icons (safe)
const emailIcon = document.getElementById("emailRefreshIcon");
if (emailIcon) emailIcon.addEventListener("click", () => triggerSpin(emailIcon));
const inboxIcon = document.getElementById("inboxRefreshIcon");
if (inboxIcon) inboxIcon.addEventListener("click", () => triggerSpin(inboxIcon));
function triggerSpin(icon) {
if (!icon) return;
icon.classList.remove("animate-spin");
void icon.offsetWidth;
icon.classList.add("animate-spin");
}
function refreshInbox() {
const icon = document.getElementById("inboxRefreshIcon");
if (!icon) return;
icon.classList.remove("icon-animate-refresh");
void icon.offsetWidth;
icon.classList.add("icon-animate-refresh");
checkInbox().then(() => {
setTimeout(() => icon.classList.remove("icon-animate-refresh"), 800);
});
}
// Read message tracking
function getReadMessages() {
const stored = localStorage.getItem("tm_read_messages");
return stored ? JSON.parse(stored) : [];
}
function markMessageAsRead(messageId) {
const readMessages = getReadMessages();
if (!readMessages.includes(messageId)) {
readMessages.push(messageId);
localStorage.setItem("tm_read_messages", JSON.stringify(readMessages));
}
}
async function checkInbox() {
if (!account) return;
const inbox = document.getElementById("inbox");
if (!inbox) return;
inbox.innerHTML = "<p>Loading...</p>";
try {
const inboxResult = await apiRequest("/messages", { method: "GET" });
if (inboxResult.response.status === 401) {
resetSessionState(false);
inbox.innerHTML = "<p>Session expired. Generate a new email.</p>";
showAlert("Session expired. Please generate a new email.");
return;
}
if (!inboxResult.response.ok) { inbox.innerHTML = "<p>Failed to load inbox.</p>"; showAlert("Failed to load inbox"); return; }
const inboxData = inboxResult.data;
const messages = inboxData["hydra:member"];
inbox.innerHTML = "";
if (messages.length === 0) { inbox.innerHTML = "<p>No messages yet.</p>"; return; }
const readMessages = getReadMessages();
for (let msg of messages) {
const receivedDate = new Date(msg.createdAt).toLocaleString();
const messageDiv = document.createElement("div");
messageDiv.classList.add("message");
const isRead = msg.hasAttachments || msg.seen || readMessages.includes(msg.id);
messageDiv.classList.add(isRead ? "read" : "unread");
messageDiv.dataset.messageId = msg.id;
const header = document.createElement("div");
header.classList.add("message-header");
header.innerHTML = `
<strong>From:</strong> ${msg.from.address}<br>
<strong>Subject:</strong> ${msg.subject}<br>
<strong>Time:</strong> ${receivedDate}<br>
<strong>Preview:</strong> ${msg.intro}
`;
messageDiv.appendChild(header);
messageDiv.onclick = () => showMessage(msg.id, messageDiv);
inbox.appendChild(messageDiv);
}
} catch (error) {
console.error(error);
inbox.innerHTML = "<p>Error loading messages.</p>";
showAlert("Error loading inbox");
}
}
async function showMessage(id, div) {
if (!div) return;
div.classList.remove("unread"); div.classList.add("read");
markMessageAsRead(id);
let bodyDiv = div.querySelector(".message-body");
if (bodyDiv) { bodyDiv.remove(); return; }
try {
const messageResult = await apiRequest(`/messages/${id}`, { method: "GET" });
if (!messageResult.response.ok) throw new Error("Failed to fetch message");
const data = messageResult.data;
const body = data.text || "No message content.";
const newDiv = document.createElement("div");
newDiv.classList.add("message-body");
const contentDiv = document.createElement("div");
contentDiv.classList.add("message-content");
contentDiv.innerHTML = linkify(body);
const controlsDiv = document.createElement("div");
controlsDiv.classList.add("message-controls");
const closeButton = document.createElement("button");
closeButton.classList.add("message-close");
closeButton.innerHTML = '<i class="fas fa-times"></i>';
closeButton.onclick = (e) => { e.stopPropagation(); newDiv.remove(); };
const copyButton = document.createElement("button");
copyButton.classList.add("message-copy");
copyButton.innerHTML = '<i class="fas fa-copy"></i>';
copyButton.onclick = (e) => {
e.stopPropagation();
navigator.clipboard.writeText(body).then(() => showAlert("Message content copied!")).catch(() => showAlert("Failed to copy"));
};
controlsDiv.appendChild(copyButton);
controlsDiv.appendChild(closeButton);
newDiv.appendChild(contentDiv);
newDiv.appendChild(controlsDiv);
newDiv.onclick = (e) => e.stopPropagation();
div.appendChild(newDiv);
} catch (error) {
console.error(error);
showAlert("Failed to load message content");
}
}
async function deleteAccount() {
if (!account) { showAlert("No active email to delete"); return; }
const deleteIcon = document.querySelector(".action-button.delete i");
if (deleteIcon) { deleteIcon.classList.remove("icon-animate-delete"); void deleteIcon.offsetWidth; deleteIcon.classList.add("icon-animate-delete"); }
try {
await apiRequest("/logout", { method: "POST" });
} catch (error) {
console.error("Logout error", error);
}
const inbox = document.getElementById("inbox");
if (inbox) inbox.innerHTML = "<p>No messages yet.</p>";
const emailDisplay = document.getElementById("emailDisplay");
if (emailDisplay) emailDisplay.innerText = "---";
resetSessionState(false);
showAlert("Email address deleted!");
}
const yearEl = document.getElementById("currentYear");
if (yearEl) yearEl.textContent = new Date().getFullYear();
// Mobile menu toggle
const mobileMenuBtn = document.querySelector(".mobile-menu-btn");
const mainNav = document.querySelector(".main-nav");
if (mobileMenuBtn && mainNav) {
mobileMenuBtn.addEventListener("click", () => mainNav.classList.toggle("show"));
}
// Close mobile menu when clicking a nav link
document.querySelectorAll(".main-nav a").forEach(link => {
link.addEventListener("click", () => { mainNav?.classList.remove("show"); });
});
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute("href"));
if (target) { target.scrollIntoView({ behavior: "smooth", block: "start" }); }
document.querySelectorAll(".main-nav a").forEach(l => l.classList.remove("active"));
this.classList.add("active");
mainNav?.classList.remove("show");
});
});
// Update active menu on scroll
window.addEventListener("scroll", () => {
const sections = document.querySelectorAll("section, div[id]");
const navLinks = document.querySelectorAll(".main-nav a");
let currentSection = "";
sections.forEach(section => {
const top = section.offsetTop;
const height = section.clientHeight;
if (window.pageYOffset >= top - 60) currentSection = section.getAttribute("id");
});
navLinks.forEach(link => {
link.classList.remove("active");
if (link.getAttribute("href").substring(1) === currentSection) link.classList.add("active");
});
});
function linkify(text) { return text.replace(/(https?:\/\/[^\s]+)/g, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>'); }
window.addEventListener("DOMContentLoaded", () => {
const savedEmail = localStorage.getItem("tm_email");
if (savedEmail) {
account = { address: savedEmail };
const emailDisplay = document.getElementById("emailDisplay");
if (emailDisplay) emailDisplay.innerText = savedEmail;
checkInbox();
inboxInterval = setInterval(checkInbox, 15000);
}
});