-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
128 lines (106 loc) · 4.33 KB
/
script.js
File metadata and controls
128 lines (106 loc) · 4.33 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
document.addEventListener('DOMContentLoaded', () => {
const emailForm = document.getElementById('emailForm');
const fromEmailInput = document.getElementById('fromEmail');
const toEmailInput = document.getElementById('toEmail');
const resultDiv = document.getElementById('result');
const generatedEmailEl = document.getElementById('generatedEmail');
const copyBtn = document.getElementById('copyBtn');
const mailtoLink = document.getElementById('mailtoLink');
const resetBtn = document.getElementById('resetBtn');
const copyMessage = document.getElementById('copyMessage');
const themeToggle = document.getElementById('themeToggle');
// Theme handling
function setTheme(theme) {
if (theme === 'auto') {
localStorage.removeItem('theme');
document.documentElement.removeAttribute('data-theme');
} else {
document.documentElement.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
}
}
function getEffectiveTheme() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
return savedTheme;
}
return window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
}
function toggleTheme() {
const currentTheme = getEffectiveTheme();
setTheme(currentTheme === 'dark' ? 'light' : 'dark');
}
function initTheme() {
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
document.documentElement.setAttribute('data-theme', savedTheme);
}
// Add system preference class for icon control
if (window.matchMedia('(prefers-color-scheme: dark)').matches) {
document.documentElement.classList.add('dark-mode-system');
} else {
document.documentElement.classList.add('light-mode-system');
}
}
// Initialize theme
initTheme();
// Add theme toggle listener
themeToggle.addEventListener('click', toggleTheme);
emailForm.addEventListener('submit', (e) => {
e.preventDefault();
const fromEmail = fromEmailInput.value.trim();
const toEmail = toEmailInput.value.trim();
if (!isValidEmail(fromEmail) || !fromEmail.endsWith('@duck.com')) {
alert('Please enter a valid DuckDuckGo email address ending with @duck.com');
return;
}
if (!isValidEmail(toEmail)) {
alert('Please enter a valid recipient email address');
return;
}
const generatedEmail = generateDDGEmail(fromEmail, toEmail);
// Display the result
generatedEmailEl.value = generatedEmail;
resultDiv.classList.remove('hidden');
// Update mailto link
mailtoLink.href = `mailto:${generatedEmail}`;
});
// Fixed to use .value instead of .textContent
copyBtn.addEventListener('click', () => {
copyToClipboard();
});
// Add click handler to the input field
generatedEmailEl.addEventListener('click', () => {
copyToClipboard();
});
// Helper function to copy and show message
function copyToClipboard() {
navigator.clipboard.writeText(generatedEmailEl.value)
.then(() => {
copyMessage.classList.remove('hidden');
setTimeout(() => {
copyMessage.classList.add('hidden');
}, 2000);
})
.catch(err => {
console.error('Could not copy text: ', err);
});
}
resetBtn.addEventListener('click', () => {
emailForm.reset();
resultDiv.classList.add('hidden');
copyMessage.classList.add('hidden');
});
function generateDDGEmail(fromEmail, toEmail) {
// Extract username from DDG email (part before @duck.com)
const username = fromEmail.split('@')[0];
// Format the "to" email by replacing @ with _at_
const formattedTo = toEmail.replace('@', '_at_');
// Construct the final email
return `${formattedTo}_${username}@duck.com`;
}
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
});