-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
40 lines (37 loc) · 1.31 KB
/
app.js
File metadata and controls
40 lines (37 loc) · 1.31 KB
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
const link = document.querySelector('#link')
const generateBtn = document.querySelector('#generateLink')
const copyBtn = document.querySelector('#copyLink')
addEventListeners();
function addEventListeners() {
generateBtn.addEventListener('click', () => {
if(copyBtn.style.display && copyBtn.style.display !== "none") {
copyBtn.style.display = "none"
generateBtn.innerHTML = "Generate Link"
link.value = null
return
}
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 1) {
link.disabled = true
link.value = "Shortning... Please wait!"
}
if (this.readyState == 4 && this.status == 201) {
const result = JSON.parse(this.response)
showShortLink(result.result.full_short_link2)
link.disabled = false
}
};
xhttp.open("GET", "https://api.shrtco.de/v2/shorten?url=" + link.value, true);
xhttp.send();
})
copyBtn.addEventListener('click', () => {
link.select();
document.execCommand('copy')
})
}
function showShortLink(shortLink) {
link.value = shortLink
copyBtn.style.display = "block"
generateBtn.innerHTML = "Generate More"
}