This repository has been archived by the owner on Nov 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
77 lines (75 loc) · 2.53 KB
/
main.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
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
const actionBtn = document.getElementById("action-btn");
const inputControl = document.getElementById("input");
const inputField = document.getElementById("input-field");
const textareaControl = document.getElementById("textarea");
const textareaField = document.getElementById("textarea-field");
const labelField = document.getElementById("label");
const linkField = document.getElementById("link-field");
const url = new URL(document.location.href);
const cmp = url.pathname.split("/");
switch (cmp.length > 1 && cmp[1] == "d" ? "decrypt" : "encrypt") {
case "encrypt":
inputField.style.display = "block";
actionBtn.onclick = () => {
const password = inputControl.value;
const content = textareaControl.value;
fetch("/api/encrypt", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ password, content }),
})
.then((res) => res.json())
.catch((err) => {
error: err.message;
})
.then((data) => {
if (data.url) {
labelField.innerHTML = `<a href="${data.url}">${data.url}</a>`;
} else if (data.error) {
labelField.innerHTML = data.error;
labelField.style.color = "red";
}
linkField.style.display = "block";
textareaField.style.display = "none";
inputControl.style.display = "none";
actionBtn.innerHTML = "Copy";
actionBtn.onclick = () => {
navigator.clipboard.writeText(data.url);
};
});
};
break;
case "decrypt":
document.getElementById("textarea-title").innerHTML = "Decrypted Content:";
const key = cmp[2];
const password = url.searchParams.get("pwd");
fetch("/api/decrypt", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ key, password }),
})
.then((res) => res.json())
.catch((err) => {
error: err.message;
})
.then((data) => {
if (data.content) {
textareaControl.innerHTML = data.content;
} else if (data.error) {
textareaControl.innerHTML = data.error;
textareaControl.style.color = "red";
}
textareaControl.style.display = "block";
inputControl.style.display = "none";
linkField.style.display = "none";
actionBtn.innerHTML = "Copy";
actionBtn.onclick = () => {
navigator.clipboard.writeText(data.content);
};
});
break;
}