-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
31 lines (26 loc) · 1009 Bytes
/
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
async function shortenUrl() {
const urlInput = document.getElementById('url-input');
const shortenBtn = document.getElementById('shorten-btn');
const shortenedUrl = document.getElementById('shortened-url');
shortenBtn.disabled = true;
shortenedUrl.innerText = 'Shortening...';
const url = urlInput.value;
const apiUrl = `https://api.shrtco.de/v2/shorten?url=${encodeURIComponent(url)}`;
try {
const response = await fetch(apiUrl);
const data = await response.json();
if (response.ok) {
shortenedUrl.innerHTML = `
Shortened URL:
<a href="${data.result.full_short_link}" target="_blank">${data.result.full_short_link}</a>
`;
} else {
shortenedUrl.innerText = 'Error shortening URL';
}
} catch (error) {
shortenedUrl.innerText = 'Error shortening URL';
} finally {
shortenBtn.disabled = false;
urlInput.value = '';
}
}