-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
72 lines (63 loc) · 2.07 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const Links = JSON.parse(localStorage.getItem("link") || "[]");
let inputUrl = document.getElementById("inputUrl");
let shortBtn = document.getElementById("shortBtn");
let slug = document.getElementById("slug");
let form = document.querySelector("form");
let linkContainer = document.querySelector(".links-container");
const spinner =
'<svg aria-hidden="true" width="24" height="24" viewBox="0 0 24 24"><use href="#icon.spinner"></use></svg>';
form.addEventListener("submit", (e) => {
e.preventDefault();
if (inputUrl.value) {
FetchData();
}else{
}
});
function displayLinks() {
document.startViewTransition(() => {
document.querySelectorAll(".links-wrapper").forEach((li) => li.remove());
let reversedLinks = [...Links].reverse();
reversedLinks.forEach((item, id) => {
let newLink = `<div class="links-wrapper">
<div class="original-link">${item.long}</div>
<a href="${item.link}" class="shorten-link">${item.link}</a>
<button onclick="copyLink(this,'${item.link}')" class="copy-btn">Copy</button>
</div>`;
linkContainer.insertAdjacentHTML("beforeend", newLink);
});
});
}
displayLinks();
function addLinks(link, long) {
let newLink = { link, long };
Links.push(newLink);
localStorage.setItem("link", JSON.stringify(Links));
displayLinks();
}
function copyLink(elem, link) {
navigator.clipboard.writeText(link);
elem.innerHTML = "Copied!";
setTimeout(() => {
elem.innerHTML = "Copy";
}, 5000);
}
function FetchData() {
if (!inputUrl.value) return;
shortBtn.innerHTML = `${spinner}`;
let mainUrl = inputUrl.value;
let newUrl = slug.value.trim();
let API = `https://csclub.uwaterloo.ca/~phthakka/1pt/addURL.php?url=${mainUrl}&cu=${newUrl}`;
fetch(API)
.then((res) => res.json())
.then((data) => {
if (data.status === 201) {
let url = "https://1pt.co/" + data.short;
addLinks(url, mainUrl);
inputUrl.value = "";
slug.value = ""
shortBtn.innerHTML = "Shorten It";
} else {
alert("Something went wrong ):");
}
});
}