-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (129 loc) · 5.58 KB
/
Copy pathscript.js
File metadata and controls
148 lines (129 loc) · 5.58 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
document.addEventListener('DOMContentLoaded', () => {
// DOM Elements
const emailDisplay = document.getElementById('temp-email-address');
const copyButton = document.getElementById('copy-button');
const refreshButton = document.getElementById('refresh-button');
const newAddressButton = document.getElementById('new-address-button');
const countdownSpan = document.getElementById('countdown');
const emailListUl = document.getElementById('email-list');
const viewerPlaceholder = document.getElementById('viewer-placeholder');
const viewerContent = document.getElementById('viewer-content');
const emailFromSpan = document.getElementById('email-from');
const emailSubjectSpan = document.getElementById('email-subject');
const emailDateSpan = document.getElementById('email-date');
const emailBodyDiv = document.getElementById('email-body');
// API
const API_BASE_URL = 'https://www.1secmail.com/api/v1/';
// State
let currentEmail = null;
let inboxInterval = null;
let countdownInterval = null;
let countdownValue = 15;
const generateNewAddress = async () => {
resetState();
emailDisplay.value = 'Generating...';
try {
const response = await fetch(`${API_BASE_URL}?action=genRandomMailbox&count=1`);
if (!response.ok) throw new Error('Failed to fetch new address.');
const data = await response.json();
currentEmail = data[0];
emailDisplay.value = currentEmail;
startCheckingInbox();
} catch (error) {
console.error(error);
emailDisplay.value = 'Error generating address.';
}
};
const checkInbox = async () => {
if (!currentEmail) return;
const [login, domain] = currentEmail.split('@');
try {
const response = await fetch(`${API_BASE_URL}?action=getMessages&login=${login}&domain=${domain}`);
if (!response.ok) throw new Error('Failed to fetch inbox.');
const messages = await response.json();
displayEmails(messages);
} catch (error) {
console.error(error);
}
};
const displayEmails = (messages) => {
emailListUl.innerHTML = '';
if (messages.length === 0) {
const li = document.createElement('li');
li.className = 'placeholder';
li.textContent = 'Inbox is empty.';
emailListUl.appendChild(li);
} else {
messages.forEach(message => {
const li = document.createElement('li');
li.dataset.id = message.id;
li.innerHTML = `<span class="from">${message.from}</span><span class="subject">${message.subject}</span>`;
li.addEventListener('click', () => viewEmail(message.id));
emailListUl.appendChild(li);
});
}
};
const viewEmail = async (id) => {
// Highlight selected email
document.querySelectorAll('#email-list li').forEach(li => li.classList.remove('selected'));
document.querySelector(`#email-list li[data-id='${id}']`).classList.add('selected');
viewerPlaceholder.classList.add('hidden');
viewerContent.classList.add('hidden');
const [login, domain] = currentEmail.split('@');
try {
const response = await fetch(`${API_BASE_URL}?action=readMessage&login=${login}&domain=${domain}&id=${id}`);
if (!response.ok) throw new Error('Failed to fetch email content.');
const emailData = await response.json();
emailFromSpan.textContent = emailData.from;
emailSubjectSpan.textContent = emailData.subject;
emailDateSpan.textContent = emailData.date;
// Use textContent for the body to prevent XSS from HTML emails
emailBodyDiv.textContent = emailData.textBody;
viewerContent.classList.remove('hidden');
} catch (error) {
console.error(error);
}
};
const startCheckingInbox = () => {
clearInterval(inboxInterval);
clearInterval(countdownInterval);
checkInbox(); // Check immediately
inboxInterval = setInterval(checkInbox, 15000);
countdownValue = 15;
countdownSpan.textContent = `(${countdownValue})`;
countdownInterval = setInterval(() => {
countdownValue--;
countdownSpan.textContent = `(${countdownValue})`;
if (countdownValue <= 0) {
countdownValue = 15;
}
}, 1000);
};
const resetState = () => {
clearInterval(inboxInterval);
clearInterval(countdownInterval);
currentEmail = null;
emailListUl.innerHTML = '<li class="placeholder">Waiting for emails...</li>';
viewerContent.classList.add('hidden');
viewerPlaceholder.classList.remove('hidden');
countdownSpan.textContent = '(15)';
};
// Event Listeners
copyButton.addEventListener('click', () => {
if (!currentEmail) return;
navigator.clipboard.writeText(currentEmail).then(() => {
const originalText = copyButton.innerHTML;
copyButton.innerHTML = 'Copied!';
setTimeout(() => {
copyButton.innerHTML = originalText;
}, 1500);
});
});
refreshButton.addEventListener('click', () => {
if (!currentEmail) return;
startCheckingInbox();
});
newAddressButton.addEventListener('click', generateNewAddress);
// Initial Load
generateNewAddress();
});