-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
100 lines (93 loc) · 2.57 KB
/
app.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
99
100
// variables
const shortBtn = document.getElementById("shortBtn");
const removeBtn = document.getElementById("removeBtn");
const inputUrl = document.getElementById("inputUrl");
const shortUrl = document.getElementById("shortUrl");
const clipBoard = document.getElementById("clipBoard");
const goLink = document.getElementById("goLink");
const goSource = document.getElementById("goSource");
const content = document.getElementById("content");
const results = document.getElementById("results");
const getData = async () => {
const response = await fetch(
`https://tinyurl.com/api-create.php?url=` +
encodeURIComponent(inputUrl.value)
);
if (response.status == 200) {
const data = await response.text();
shortUrl.value = data;
} else {
inputUrl.value = "";
content.innerHTML = `<h1 style="text-align:center; color: #fff">Invalid URL ‼</h1>`;
const body = document.querySelector("body");
body.style.flexDirection = "column";
setTimeout(() => {
content.innerHTML = ``;
body.style.flexDirection = "row";
}, 2000);
}
};
inputUrl.addEventListener("focus",()=> inputUrl.value = "")
shortBtn.addEventListener("click", getData);
removeBtn.addEventListener("click", () => {
shortUrl.value = "";
inputUrl.value = "";
goSource.href = "#";
goSource.target = "";
});
goLink.addEventListener("click", () => {});
const copyClip = () => {
if (shortUrl.value.trim() ) {
const url = document.getElementById("shortUrl");
url.select();
document.execCommand("copy");
results.textContent = "Copied ✅";
results.style.color = "green";
results.style.textAlign = "end";
timeOut();
} else {
results.textContent = "No link 👻";
results.style.color = "#fff";
results.style.textAlign = "end";
timeOut();
}
};
clipBoard.addEventListener("click", copyClip);
shortUrl.addEventListener("input", () => {
shortUrl.value = shortUrl.value.slice(0, -1);
});
goSource.addEventListener("click", () => {
if (shortUrl.value) {
goSource.href = shortUrl.value;
goSource.target = "_blank";
} else {
if(inputUrl.value && !shortUrl.value){
Swal.fire({
position: "center",
icon: "info",
title: "Short it before go link!",
showConfirmButton: false,
timer: 1000,
});
}else{
swal()
}
}
});
const swal = () => {
Swal.fire({
position: "center",
icon: "question",
title: "No link found!",
showConfirmButton: false,
timer: 1000,
});
};
const timeOut = () => {
setTimeout(() => {
results.textContent = "";
results.style.color = "#000";
results.style.textAlign = "";
}, 1000);
};
// Try link for debugging : //https://www.youtube.com/watch?v=foa1jeLLXI0