-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
40 lines (34 loc) · 1019 Bytes
/
index.js
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
const inputsDiv = document.getElementById("inputs");
const resultCode = document.getElementById("result");
const copyButton = document.getElementById("copy");
const inputElements = inputsDiv.getElementsByTagName("input");
function getUserInput() {
const inputs = {};
for (const i of inputElements) {
inputs[i.id] = i.value;
}
return inputs;
}
let template;
function init() {
template = resultCode.textContent;
updateResult();
resultCode.style.visibility = "visible";
}
function updateResult() {
const inputs = getUserInput();
let result = template.slice();
for (const [key, val] of Object.entries(inputs)) {
const pattern = new RegExp(`\\$\{${key}\}`, "g");
result = result.replaceAll(pattern, val);
}
resultCode.textContent = result;
}
function copyResult() {
let result = resultCode.textContent.trim();
result += "\n";
navigator.clipboard.writeText(result);
}
init();
inputsDiv.addEventListener("input", updateResult);
copyButton.addEventListener("click", copyResult);