-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
82 lines (74 loc) · 2.62 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
$(document).ready(function() {
$(window).scroll(function() {
if (this.scrollY > 20) {
console.log("I'm in scrolling motion")
$('.navbar').addClass('sticky');
}
else {
$('.navbar').removeClass('sticky');
}
})
// toggle menu/navbar active state
$('.menu-btn').click(function() {
$('.navbar .menu').toggleClass("active");
$('.menu-btn i').toggleClass("active");
})
})
button = document.getElementById('submit')
url = document.getElementById('url')
code = document.getElementById('code')
result = document.getElementById('result')
async function shortenURL() {
button.disabled = true
urlToShorten = url.value
customCode = code.value
console.log(`URL: ${urlToShorten}\nCode: ${customCode}`);
let shortenedURL = ':)'
let requestURL = "https://shortt-ly.herokuapp.com/api/custom"
// let requestURL = "http://localhost:5000/api/custom"
let requestBody = {
longUrl: urlToShorten,
urlCode: customCode
}
if(customCode == '') {
requestURL = "https://shortt-ly.herokuapp.com/api/short"
// requestURL = "http://localhost:5000/api/short"
requestBody = {
longUrl: urlToShorten
}
}
console.log(customCode)
// Make an API call to the URL shortening service and get the json and display the shortened URL or errors(if any)
// await fetch("https://shortt-ly.herokuapp.com/api/short", {
await fetch(requestURL, {
method: 'POST',
body: JSON.stringify(requestBody),
headers: {
"Content-type": "application/json; charset=UTF-8"
}
}).then(response => response.json())
.then(json => {
console.log(json)
if(json.hasOwnProperty('errors')) {
let displayText = `Error: ${json.errors.message}`
result.innerHTML = displayText
}
else {
shortenedURL = json.shortUrl;
let displayText = shortenedURL
result.innerHTML = `<a id="short-url" href="` + displayText + `" target="_blank">` + displayText + `</a><i class="material-icons" onclick="copyToClipboard()">content_copy</i>`
}
})
.catch(err => console.log(err));
button.disabled = false
}
function copyToClipboard() {
let short_url = document.getElementById('short-url')
const el = document.createElement('textarea');
el.value = short_url.innerText;
document.body.appendChild(el);
el.select();
document.execCommand('copy');
document.body.removeChild(el);
console.log(short_url.innerText)
}