-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpopup.js
More file actions
117 lines (102 loc) · 4.26 KB
/
popup.js
File metadata and controls
117 lines (102 loc) · 4.26 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
document.addEventListener('DOMContentLoaded', loadDataFromStorage);
document.getElementById('usernameInput').addEventListener('input', updateResults);
document.getElementById('emailInput').addEventListener('input', updateResults);
document.getElementById('copyAllButton').addEventListener('click', copyAllEmails);
// if browser == chrome, storage = chrome.storage
function addonLog(...args) {
// prepend extension name
console.log('duck_mail_converter:', args);
}
var inChrome = (typeof chrome !== 'undefined');
addonLog(inChrome);
const storage = inChrome ? chrome.storage.sync : browser.storage.sync;
async function loadStorageBrowserCompat(keys) {
if (inChrome) {
return new Promise(resolve => storage.get(keys, resolve));
}
return storage.local.get(keys);
}
function getAllEmails() {
const emails = Array.from(document.querySelectorAll('.result-item span')).map(span => span.textContent).filter(email => email);
const joinedEmails = emails.join(';');
return joinedEmails;
}
function copyAllEmails() {
const element = document.getElementById('copyAllButton');
navigator.clipboard.writeText(element.title).then(() => {
addonLog("copy all emails done.");
}).catch(err => {
addonLog('Something went wrong copying all emails', err);
});
}
function updateJoinedbuttons() {
const joinedEmails = getAllEmails();
document.getElementById("copyAllButton").title = joinedEmails;
const mailAllElement = document.getElementById('mailtoAllButton');
mailAllElement.href = "mailto:" + joinedEmails;
mailAllElement.title = mailAllElement.href;
Array.from(document.getElementsByClassName("copy-button")).forEach(button => {
button.addEventListener('click', (event) => {
const tgt = event.currentTarget;
// https://stackoverflow.com/questions/57278923/chrome-76-copy-content-to-clipboard-using-navigator
const blob = new Blob([tgt.title], { type: 'text/plain' });
const item = new ClipboardItem({ 'text/plain': blob });
navigator.clipboard.write([item]).catch(error => {
console.error("unable to write to clipboard. Error:", error);
});
}, false);
});
}
async function loadDataFromStorage() {
const data = await loadStorageBrowserCompat(['username', 'emailInput']);
const username = data['username'];
const emailInput = data['emailInput'];
if (username) {
document.getElementById('usernameInput').value = username;
}
if (emailInput) {
document.getElementById('emailInput').value = emailInput;
updateResults(); // Update results with the loaded username
}
}
function updateResults() {
const usernameInput = document.getElementById('usernameInput');
const username = usernameInput.value.trim();
const emailInput = document.getElementById('emailInput').value.trim();
const resultContainer = document.getElementById('resultContainer');
resultContainer.innerHTML = '';
// Save the username to storage
storage.set({ 'username': username, 'emailInput': emailInput });
if (!emailInput) {
return;
}
const emails = emailInput.split(/ |\n/);
emails.forEach(email => {
const convertedEmail = convertEmail(email, username);
if (!convertedEmail) {
return;
}
const resultRow = document.createElement('tr');
resultRow.innerHTML = `
<td class="result-item"> <span>${convertedEmail}</span></td>
<td><button class="copy-button" title="${convertedEmail}">Copy</button></td>
<td><a class="mailto-link" href="mailto:${convertedEmail}" title="mailto:${convertedEmail}">Mail</a></td>
`;
resultContainer.appendChild(resultRow);
});
updateJoinedbuttons();
}
function convertEmail(email, username) {
const formattedEmail = email
.replace(/\[at\]/gi, '@')
.replace(/\[dot\]/gi, '.')
.trim();
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (emailPattern.test(formattedEmail)) {
const split = formattedEmail.split('@');
const localPart = split[0];
const originalDomain = split[1];
return `${localPart}_at_${originalDomain}_${username}@duck.com`;
}
return null;
}