forked from hestiacp/hestiacp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopyCreds.js
More file actions
61 lines (48 loc) · 1.87 KB
/
copyCreds.js
File metadata and controls
61 lines (48 loc) · 1.87 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
// Monitor "Account" and "Password" inputs on "Add/Edit Mail Account"
// page and update the sidebar "Account" and "Password" output
export default function handleCopyCreds() {
monitorAndUpdate('.js-account-input', '.js-account-output');
monitorAndUpdate('.js-password-input', '.js-password-output');
}
function monitorAndUpdate(inputSelector, outputSelector) {
const inputElement = document.querySelector(inputSelector);
const outputElement = document.querySelector(outputSelector);
if (!inputElement || !outputElement) {
return;
}
function updateOutput(value) {
outputElement.textContent = value;
generateMailCredentials();
}
inputElement.addEventListener('input', (event) => {
updateOutput(event.target.value);
});
updateOutput(inputElement.value);
}
// Update hidden input field with values from cloned email info panel
function generateMailCredentials() {
const mailInfoPanel = document.querySelector('.js-mail-info');
if (!mailInfoPanel) return;
const formattedCredentials = emailCredentialsAsPlainText(mailInfoPanel.cloneNode(true));
document.querySelector('.js-hidden-credentials').value = formattedCredentials;
}
// Reformats cloned DOM email credentials into plain text
function emailCredentialsAsPlainText(element) {
const headings = [...element.querySelectorAll('h2')];
const lists = [...element.querySelectorAll('ul')];
return headings
.map((heading, index) => {
const items = [...lists[index].querySelectorAll('li')];
const itemText = items
.map((item) => {
const label = item.querySelector('.values-list-label');
const value = item.querySelector('.values-list-value');
const valueLink = value.querySelector('a');
const valueText = valueLink ? valueLink.href : value.textContent;
return `${label.textContent}: ${valueText}`;
})
.join('\n');
return `${heading.textContent}\n${itemText}\n`;
})
.join('\n');
}