-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
54 lines (48 loc) · 1.43 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
let backendURL = `https://url-shortener-ii67.onrender.com/url`;
let originalURL = document.getElementById("long-url");
let submitBtn = document.getElementById("submit");
let errorDiv = document.getElementById("error");
let newURL = document.getElementById("shorturl");
let statusDiv = document.getElementById("status");
submitBtn.addEventListener("click", () => {
if (originalURL.value == "") {
errorDiv.innerText = "Please Enter your URL";
} else {
getShortURL(originalURL.value);
}
});
async function getShortURL(url) {
try {
let res = await fetch(`${backendURL}/original`, {
method: "POST",
headers: {
"Content-type": "application/json",
},
body: JSON.stringify({
originalURL: url,
}),
});
let data = await res.json();
console.log(data.newURL);
newURL.textContent = data.newURL;
} catch (error) {
console.log(error);
}
}
//copying shorturl to clipboard
const copyButton = document.getElementById("copy");
if (copyButton && navigator.clipboard) {
copyButton.addEventListener("click", async () => {
const textToCopy = newURL.textContent;
try {
await navigator.clipboard.writeText(textToCopy);
statusDiv.innerText = "URL copied to clipboard";
} catch (err) {
console.error("Unable to copy text to clipboard.", err);
}
});
} else {
console.error(
"Clipboard API not supported or copy button not found in this browser."
);
}