-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
90 lines (80 loc) · 2.98 KB
/
index.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
let btnShortenIt = document.getElementById('btnShortenIt')
let userUrl = document.getElementById('userUrl')
let dynamicContent = document.getElementById('dynamicContent')
let hamburger = document.getElementById('hamburger')
hamburger.addEventListener('click', function (e) {
let mobileMenu = document.getElementById('mobile-menu')
mobileMenu.classList.toggle('hidden')
})
document.addEventListener('DOMContentLoaded', function () {
var clipboard = new ClipboardJS('.btn-copy')
clipboard.on('success', function (e) {
// console.info('Action:', e.action);
// console.info('Text:', e.text);
// console.info('Trigger:', e.trigger);
e.trigger.textContent = 'Copied!'
e.trigger.classList.add('bg-black', 'hover:bg-black')
})
clipboard.on('error', function (e) {
alert('Not Copied! Please try again.')
// console.info('Action:', e.action);
// console.info('Text:', e.text);
// console.info('Trigger:', e.trigger);
})
})
btnShortenIt.addEventListener('click', function () {
// console.log(userUrl.value)
fetch(`https://api.shrtco.de/v2/shorten?url=${userUrl.value}`)
.then((response) => response.json())
.then((data) => {
dynamicContent.innerHTML = ''
displayData(
data.result.original_link,
data.result.short_link,
data.result.short_link2,
data.result.short_link3,
)
removeErrLayout()
// console.log(data)
})
.catch((err) => addErrLayout())
})
function removeErrLayout() {
let inputBox = document
.getElementById('inputBox')
.classList.remove('border-red-500')
let addLinkWarning = document
.getElementById('addLinkWarning')
.classList.add('hidden')
userUrl.classList.remove('placeholder-red-500')
}
function addErrLayout() {
let inputBox = document
.getElementById('inputBox')
.classList.add('border-red-500')
let addLinkWarning = document
.getElementById('addLinkWarning')
.classList.remove('hidden')
userUrl.classList.add('placeholder-red-500')
userUrl.value = ''
}
function displayData(originalLink, link1, link2, link3) {
const arrOfLinks = [originalLink, link1, link2, link3]
for (let i = 1; i < arrOfLinks.length; i++) {
dynamicContent.innerHTML += `
<section id="dynamicContent" class="bg-slate-100 mx-6 md:mx-0">
<div class="container mx-auto">
<div class="bg-white grid md:flex md:justify-between mt-10 rounded-xl p-5">
<div class="py-3 px-8 text-black">
<a href="${originalLink}">${originalLink}</a>
</div>
<div class="grid md:flex gap-10 ">
<a class="text-cyann py-3 px-8" target="_blank" name="shortLink" href="https://${arrOfLinks[i]}">${arrOfLinks[i]}</a>
<button id="btnCopy" data-clipboard-text="https://${arrOfLinks[i]}" class="mx-8 bg-cyann btn-copy text-white py-3 px-8 rounded-lg hover:bg-cyann2">Copy</button>
</div>
</div>
</div>
</section>
`
}
}