-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscript.js
More file actions
136 lines (123 loc) · 4.14 KB
/
script.js
File metadata and controls
136 lines (123 loc) · 4.14 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
129
130
131
132
133
134
135
136
const serverUrl = "https://extension-iota.vercel.app";
// const serverUrl = "http://localhost:4000";
var timer = document.getElementById("timer");
var totalSeconds = 1200;
function setTime() {
--totalSeconds;
timer.textContent =
pad(parseInt(totalSeconds / 60)) + ":" + pad(totalSeconds % 60);
}
function pad(val) {
var valString = val + "";
if (valString.length < 2) {
return "0" + valString;
} else {
return valString;
}
}
// paste text from clipboard to textarea on click
const content = document.getElementById("content");
content.addEventListener("click", function () {
// if content is already present, do not paste from clipboard
if (content.value === "") {
navigator.clipboard
.readText()
.then((text) => (content.value = text))
.catch((err) =>
console.error("Failed to read clipboard contents: ", err)
);
}
});
const sendBtn = document.getElementById("send-btn");
const receiveBtn = document.getElementById("receive-btn");
const copyBtn = document.getElementById("copy-btn");
// sha-256 hashing function
async function sha256(message) {
// encode as UTF-8
const msgBuffer = new TextEncoder().encode(message);
// hash the message
const hashBuffer = await crypto.subtle.digest("SHA-256", msgBuffer);
// convert ArrayBuffer to Array
const hashArray = Array.from(new Uint8Array(hashBuffer));
// convert bytes to hex string
const hashHex = hashArray
.map((b) => b.toString(16).padStart(2, "0"))
.join("");
return hashHex;
}
// action to be performed on clicking the send button
sendBtn.addEventListener("click", async () => {
const letters = "abcdefghijklmnopqrstuvwxyz".split("");
// Generate a random 3-letter string
let cid = "";
for (let i = 0; i < 3; i++) {
const randomIndex = Math.floor(Math.random() * letters.length);
cid += letters[randomIndex];
}
// trim the textarea content to remove any extra spaces
const cnote = document.getElementById("content").value.trim();
// check if password was entered
let hash = "";
const password = document.getElementById("send-password").value.trim();
if (password !== "") {
hash = await sha256(cid + password);
}
const data = { id: cid, note: cnote, hash };
fetch(serverUrl + "/setnote", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
})
.then((response) => {
const codeBlk = document.getElementById("code-blk");
content.classList.add("d-none");
codeBlk.classList.remove("d-none");
document.getElementById("code").textContent = cid.toString();
const intervalId = setInterval(setTime, 1000);
setTimeout(() => {
clearInterval(intervalId);
content.classList.remove("d-none");
codeBlk.classList.add("d-none");
}, 1200000);
response.json();
})
.catch((error) => console.error(error));
});
// action to be performed on clicking the receive button
receiveBtn.addEventListener("click", async () => {
// trim the input to remove any extra spaces
const reqnum = document.getElementById("reqnum").value.trim();
const receivePassword = document.getElementById("rec-password").value.trim();
const invalidCodeDiv = document.getElementById("invalid-code");
let hash = "";
if (receivePassword !== "") {
hash = await sha256(reqnum + receivePassword);
}
fetch(serverUrl + `/getnote/${reqnum}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ hash }),
})
.then((response) => response.json())
.then((data) => {
if (data.error == "No note found") {
invalidCodeDiv.classList.remove("d-none");
return;
}
copyBtn.classList.remove("d-none");
invalidCodeDiv.classList.add("d-none");
content.value = data.note.note;
})
.catch((error) => console.log(error));
});
//copy button action
copyBtn.addEventListener("click", () => {
navigator.clipboard
.writeText(document.getElementById("content").value)
.then(() => {
copyBtn.style.background = "url('./assets/check-solid.svg') no-repeat";
})
.catch((err) => {
console.error("Failed to copy text: ", err);
});
});