-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
96 lines (81 loc) · 2.47 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
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
const cardContainerEl = document.getElementById("card-container");
const inputEl = document.getElementById("urlInput");
const btnEl = document.getElementById("shortenBtn");
btnEl.addEventListener("click", async () => {
const url = inputEl.value;
const warnTextEl = document.querySelector('.warning-text');
if (!url) {
warnTextEl.classList.add('active');
inputEl.classList.add('active');
return;
}
warnTextEl.classList.remove('active');
inputEl.classList.remove('active');
btnEl.textContent = "Loading...";
const response = await shortenUrl(url);
saveDataInLS(url, response.result.full_short_link);
init();
btnEl.textContent = "Shorten it!";
});
cardContainerEl.addEventListener("click", (e) => {
if (e.target.tagName !== "BUTTON") return;
const buttons = cardContainerEl.querySelectorAll(".btn");
buttons.forEach((btn) => {
btn.style.backgroundColor = "hsl(180, 66%, 49%)";
btn.textContent = "Copy";
});
const text = e.target.previousElementSibling.textContent;
copyToClipboard(text);
const copyBtn = e.target;
copyBtn.style.backgroundColor = "hsl(255, 11%, 22%)";
copyBtn.textContent = "Copied!";
});
function init() {
cardContainerEl.innerHTML = "";
const urls = getDataFromLS();
if (!urls) return;
urls.forEach((url) => renderLink(url.url, url.shortenUrl));
}
function renderLink(url, shortenUrl) {
const card = `
<div class="link-card">
<div>
<span>${url}</span>
</div>
<div>
<span>${shortenUrl}</span>
<button class="btn btn-square btn-small">Copy</button>
</div>
</div>
`;
cardContainerEl.innerHTML += card;
}
init();
async function shortenUrl(url) {
return new Promise((resolve, reject) => {
fetch(`https://api.shrtco.de/v2/shorten?url=${url}`)
.then((response) => response.json())
.then((data) => resolve(data))
.catch((err) => reject(err));
});
}
function getDataFromLS() {
return JSON.parse(localStorage.getItem("urls"));
}
function saveDataInLS(prevUrl, shortenUrl) {
const storedData = getDataFromLS() ?? [];
if (storedData.length > 2) {
storedData.pop();
}
const newData = { url: prevUrl, shortenUrl };
storedData.unshift(newData);
localStorage.setItem("urls", JSON.stringify(storedData));
}
const copyToClipboard = (str) => {
const el = document.createElement("textarea");
el.value = str;
document.body.appendChild(el);
el.select();
document.execCommand("copy");
document.body.removeChild(el);
};