-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
61 lines (51 loc) · 1.86 KB
/
script.js
File metadata and controls
61 lines (51 loc) · 1.86 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
48
49
50
51
52
53
54
55
56
57
58
59
60
async function shortenUrl() {
const urlInput = document.getElementById('url-input');
const resultContainer = document.getElementById('result');
const longUrl = urlInput.value.trim();
if (!longUrl) {
resultContainer.innerText = 'Please enter a valid URL.';
return;
}
// API endpoint for URL shortening
const apiEndpoint = 'https://api-ssl.bitly.com/v4/shorten';
// API headers with your API key
const headers = {
'Content-Type': 'application/json',
'Authorization': 'Bearer c99570487e4ffe99ae77b63de8627bb6c67d62c2' // Replace with your actual API key
};
// Request body
const requestBody = {
long_url: longUrl
};
try {
const response = await fetch(apiEndpoint, {
method: 'POST',
headers: headers,
body: JSON.stringify(requestBody)
});
if (response.ok) {
const data = await response.json();
const shortUrl = data.link; // The shortened URL
// Display the shortened URL and the "Copy" button
resultContainer.innerHTML = `
Shortened URL: <a href="${shortUrl}" target="_blank">${shortUrl}</a>
<button onclick="copyToClipboard('${shortUrl}')">Copy</button>
`;
} else {
const errorData = await response.json();
resultContainer.innerText = `Error: ${errorData.message}`;
}
} catch (error) {
resultContainer.innerText = `Error: ${error.message}`;
}
}
function copyToClipboard(shortUrl) {
// Use the Clipboard API to copy the short URL to the clipboard
navigator.clipboard.writeText(shortUrl)
.then(() => {
alert('Shortened URL copied to clipboard!');
})
.catch((err) => {
console.error('Failed to copy text to clipboard:', err);
});
}