-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
54 lines (50 loc) · 1.7 KB
/
script.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
const messageForm = document.querySelector("form");
const messageLink = document.querySelector(".link");
const copyBtn = document.querySelector("#copy-btn");
const root = document.querySelector("#root");
const title = document.querySelector(".title");
const messageFormSection = document.querySelector(".message-form-container");
const secretMessage = document.querySelector(".secret-message");
const resetLink = document.querySelector(".reset-link");
function setClipboard(text) {
const type = "text/plain";
const blob = new Blob([text], { type });
try {
const data = [new ClipboardItem({ [type]: blob })];
navigator.clipboard.write(data).then(function () {
window.M.toast({
html: "Copied to clipboard",
classes: "toast-clipboard",
});
});
} catch (err) {
window.M.toast({
html: `${err}`,
classes: "red",
});
}
}
messageForm.addEventListener("submit", (e) => {
e.preventDefault();
const encodedMessage = btoa(e.target[0].value);
const urlParams = new URLSearchParams();
urlParams.set("message", encodedMessage);
messageLink.value = window.location.href + "?" + urlParams;
});
window.onload = function () {
const urlParams = new URLSearchParams(window.location.search);
const encodedMessage = urlParams.get("message");
root.classList.toggle("hide");
if (encodedMessage) {
messageForm.classList.add("hide");
title.innerHTML = "Your Secret Message is";
const decodedMessage = atob(encodedMessage);
secretMessage.classList.remove("hide");
secretMessage.textContent = decodedMessage;
resetLink.classList.remove("hide");
resetLink.href = window.location.origin + window.location.pathname;
}
};
copyBtn.addEventListener("click", function () {
setClipboard(messageLink.value);
});