-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
208 lines (174 loc) · 6.56 KB
/
main.js
File metadata and controls
208 lines (174 loc) · 6.56 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
document.addEventListener('DOMContentLoaded', function() {
// DOM Elements
const urlInput = document.getElementById('url-input');
const shortenBtn = document.getElementById('shorten-btn');
const resultContainer = document.querySelector('.result-container');
const originalUrlText = document.getElementById('original-url-text');
const shortenedUrl = document.getElementById('shortened-url');
const copyBtn = document.getElementById('copy-btn');
const shareBtns = document.querySelectorAll('.share-btn');
const qrBtn = document.getElementById('qr-btn');
const qrModal = document.querySelector('.qr-modal');
const closeBtn = document.querySelector('.close-btn');
const qrCode = document.getElementById('qr-code');
const downloadPng = document.getElementById('download-png');
const downloadSvg = document.getElementById('download-svg');
const notification = document.querySelector('.notification');
// API Configuration
const API_URL = 'https://url-shortener-service.p.rapidapi.com/shorten';
const API_KEY = '5432b8c48dmsh8788b7ecc760657p1563c6jsn15fe60d22fd7';
const API_HOST = 'url-shortener-service.p.rapidapi.com';
// Shorten URL function
async function shortenUrl(longUrl) {
try {
const response = await fetch(API_URL, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'x-rapidapi-host': API_HOST,
'x-rapidapi-key': API_KEY
},
body: `url=${encodeURIComponent(longUrl)}`
});
if (!response.ok) {
throw new Error('Failed to shorten URL');
}
const data = await response.json();
return data.result_url;
} catch (error) {
console.error('Error shortening URL:', error);
showNotification('Error shortening URL. Please try again.', true);
return null;
}
}
// Event Listeners
shortenBtn.addEventListener('click', handleShorten);
urlInput.addEventListener('keypress', function(e) {
if (e.key === 'Enter') {
handleShorten();
}
});
copyBtn.addEventListener('click', function() {
shortenedUrl.select();
document.execCommand('copy');
showNotification('URL copied to clipboard!');
});
shareBtns.forEach(btn => {
if (btn.id !== 'qr-btn') {
btn.addEventListener('click', function() {
shareUrl(btn.dataset.platform);
});
}
});
qrBtn.addEventListener('click', showQRCode);
closeBtn.addEventListener('click', hideQRCode);
downloadPng.addEventListener('click', downloadQRCodePNG);
downloadSvg.addEventListener('click', downloadQRCodeSVG);
// Click outside modal to close
window.addEventListener('click', function(event) {
if (event.target === qrModal) {
hideQRCode();
}
});
// Functions
async function handleShorten() {
const longUrl = urlInput.value.trim();
if (!longUrl) {
showNotification('Please enter a URL', true);
return;
}
// Validate URL format
try {
new URL(longUrl);
} catch (e) {
showNotification('Please enter a valid URL', true);
return;
}
// Show loading state
shortenBtn.disabled = true;
shortenBtn.textContent = 'Shortening...';
const shortUrl = await shortenUrl(longUrl);
shortenBtn.disabled = false;
shortenBtn.textContent = 'Shorten';
if (shortUrl) {
originalUrlText.textContent = longUrl;
shortenedUrl.value = shortUrl;
resultContainer.classList.remove('hidden');
urlInput.value = '';
showNotification('URL shortened successfully!');
}
}
function shareUrl(platform) {
const url = encodeURIComponent(shortenedUrl.value);
let shareUrl = '';
switch (platform) {
case 'facebook':
shareUrl = `https://www.facebook.com/sharer/sharer.php?u=${url}`;
break;
case 'twitter':
shareUrl = `https://twitter.com/intent/tweet?url=${url}`;
break;
case 'linkedin':
shareUrl = `https://www.linkedin.com/shareArticle?mini=true&url=${url}`;
break;
case 'whatsapp':
shareUrl = `https://wa.me/?text=${url}`;
break;
default:
return;
}
window.open(shareUrl, '_blank', 'width=600,height=400');
}
function showQRCode() {
const url = shortenedUrl.value;
if (!url) return;
qrCode.innerHTML = '';
QRCode.toCanvas(url, { width: 200 }, function(error, canvas) {
if (error) {
console.error('QR code generation error:', error);
showNotification('Error generating QR code', true);
return;
}
qrCode.appendChild(canvas);
});
qrModal.classList.add('show');
}
function hideQRCode() {
qrModal.classList.remove('show');
}
function downloadQRCodePNG() {
const canvas = qrCode.querySelector('canvas');
if (!canvas) return;
const link = document.createElement('a');
link.download = 'shorturl-qr.png';
link.href = canvas.toDataURL('image/png');
link.click();
}
function downloadQRCodeSVG() {
const url = shortenedUrl.value;
if (!url) return;
QRCode.toString(url, { type: 'svg' }, function(error, svg) {
if (error) {
console.error('SVG QR code generation error:', error);
showNotification('Error generating SVG QR code', true);
return;
}
const blob = new Blob([svg], { type: 'image/svg+xml' });
const link = document.createElement('a');
link.download = 'shorturl-qr.svg';
link.href = URL.createObjectURL(blob);
link.click();
});
}
function showNotification(message, isError = false) {
notification.textContent = message;
notification.classList.remove('error');
if (isError) {
notification.classList.add('error');
}
notification.classList.add('show');
setTimeout(() => {
notification.classList.remove('show');
}, 3000);
}
});