Skip to content

Commit 9ba1482

Browse files
authored
Create script.js
1 parent 1c4c2ff commit 9ba1482

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

URL Shortener/blindaks/script.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const shortenUrl = async () => {
2+
const longUrl = document.getElementById('long-url').value;
3+
const response = await fetch(`https://api.shrtco.de/v2/shorten?url=${longUrl}`);
4+
const data = await response.json();
5+
const shortUrl = data.result.full_short_link;
6+
7+
const shortUrlContainer = document.getElementById('short-url-container');
8+
const shortUrlLink = document.getElementById('short-url').querySelector('a');
9+
shortUrlLink.href = shortUrl;
10+
shortUrlLink.textContent = shortUrl;
11+
shortUrlContainer.style.display = 'block';
12+
13+
const copyBtn = document.getElementById('copy-btn');
14+
copyBtn.addEventListener('click', () => {
15+
navigator.clipboard.writeText(shortUrl).then(() => {
16+
alert('Copied to clipboard!');
17+
});
18+
});
19+
};
20+
21+
const shortenBtn = document.getElementById('shorten-btn');
22+
shortenBtn.addEventListener('click', shortenUrl);

0 commit comments

Comments
 (0)