-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
47 lines (42 loc) · 1.5 KB
/
script.js
File metadata and controls
47 lines (42 loc) · 1.5 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
41
42
43
44
45
46
47
document.getElementById('shorten').addEventListener('click', shortenURL);
document.getElementById('copy').addEventListener('click', copyURL);
function shortenURL() {
const longURL = document.querySelector('.right-card input[type="text"]').value;
if (!longURL) return;
const apiURL = `https://api.tinyurl.com/create?url=${encodeURIComponent(longURL)}`;
const apiToken = 'okpY8c4ZxjeLiXUGUQUk83F6eGe11wlJdOxFCk4Lnd6anVQ6hMrtJVOqEJUM';
fetch(apiURL, {
method: 'POST',
headers: {
'Authorization': `Bearer ${apiToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
url: longURL,
}),
})
.then(response => response.json())
.then(data => {
const outputField = document.querySelector('.left-card input[type="text"]');
if (data.data) {
const shortURL = data.data.tiny_url;
outputField.value = shortURL;
} else {
outputField.value = `Error: ${data.errors[0].message}`;
}
})
.catch(error => {
document.querySelector('.left-card input[type="text"]').value = `Error: ${error.message}`;
});
}
function copyURL() {
const outputField = document.querySelector('.left-card input[type="text"]');
outputField.select();
outputField.setSelectionRange(0, 99999);
try {
document.execCommand('copy');
alert('Short URL copied to clipboard!');
} catch (err) {
alert('Failed to copy URL!');
}
}