-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
98 lines (93 loc) · 3.01 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
97
98
//navbar
const navbar = document.querySelector(".navbar_left");
const navbarIcons = document.querySelector(".navbar_menu");
//to open and close
navbarIcons.addEventListener("click", () => {
navbar.classList.toggle("navbar_show");
});
const searchBar = document.querySelector(".searchbar");
const error = document.querySelector(".input_box p");
const inputBox = document.querySelector(".input_box input");
const button = document.querySelector(".searchbar button");
const urlDiv = document.querySelector(".url_links");
const data = localStorage.getItem("_html");
if (!data) {
localStorage.setItem("_html", "[]");
}
const searhApi = async (value) => {
if (!value || value == "") {
return searchBar.classList.add("error");
}
button.innerText = "Loading...";
button.disabled = true;
let apiUrl = "https://api.shrtco.de/v2/shorten";
let ApiReq = await fetch(apiUrl + `?url=${value}`);
let ApiRes = await ApiReq.json();
button.innerText = "Shorten it!";
button.disabled = false;
if (ApiRes.ok) {
searchBar.classList.remove("error");
const localData = JSON.parse(data);
let obj = {
userInput: value,
shortenUrl: ApiRes.result.full_short_link,
};
localData.push(obj);
localStorage.setItem("_html", JSON.stringify(localData));
showHtml();
} else {
searchBar.classList.add("error");
if (ApiRes.error_code == 2) {
error.innerText = "invalid Url";
} else {
error.innerText = ApiRes.error;
}
}
};
const showHtml = () => {
if (!localStorage.getItem("_html")) {
return;
}
let updatedData = JSON.parse(localStorage.getItem("_html"));
let html = updatedData
.reverse()
.map((data) => {
return `<div class="url_link flex items-center justify-between">
<h2><a href=${data.userInput}>${data.userInput}</a></h2>
<div class="flex items-center gap-2">
<p><a href=${data.shortenUrl}>${data.shortenUrl}</a></p>
<button data-link=${data.shortenUrl} class="copy btn-primary btn-rounded" type="button">
copy
</button>
</div>
</div>`;
})
.join("");
urlDiv.innerHTML = html;
let buttons = document.querySelectorAll(".copy");
buttons.forEach((btn) => {
btn.addEventListener("click", (e) => {
const copyText = e.target.dataset.link;
navigator.clipboard.writeText(copyText);
e.target.innerText = "Copied!";
e.target.classList.add("copied");
setTimeout(() => {
e.target.innerText = "Copy";
e.target.classList.remove("copied");
}, 1500);
});
});
};
button.addEventListener("click", () => {
searhApi(inputBox.value);
inputBox.value = null;
});
inputBox.addEventListener("focus", (e) => {
window.addEventListener("keydown", (e) => {
if (e.key == "Enter") {
searhApi(inputBox.value);
inputBox.value = null;
}
});
});
showHtml();