-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
116 lines (99 loc) · 4.42 KB
/
script.js
File metadata and controls
116 lines (99 loc) · 4.42 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
document.addEventListener('DOMContentLoaded', () => {
const shortenForm = document.getElementById('shorten-form');
const loginForm = document.getElementById('login-form');
const loginButton = document.getElementById('login-button');
const logoutButton = document.getElementById('logout-button');
const shortenedUrlElement = document.getElementById('shortened-url');
const errorMessageElement = document.getElementById('error-message');
const qrCodeImage = document.getElementById('qrCodeImage');
const customShortUrlInput = document.getElementById('customShortUrl');
// Function to check if the user is logged in (you should implement this logic)
function isLoggedIn() {
return localStorage.getItem('isLoggedIn') === 'true';
}
// Function to show or hide the custom short URL input field based on login status
function toggleCustomShortUrlField() {
const userIsLoggedIn = isLoggedIn();
customShortUrlInput.disabled = !userIsLoggedIn;
customShortUrlInput.value = ''; // Clear the input value if it was previously set
}
// Check login status when the page loads and adjust the form accordingly
toggleCustomShortUrlField();
shortenForm.addEventListener('submit', async (e) => {
e.preventDefault();
const originalUrl = document.getElementById('originalUrl').value;
const customShortUrl = document.getElementById('customShortUrl').value;
try {
// Send a POST request to your server to shorten the URL
const response = await fetch('/api/shorten', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
originalUrl,
customShortUrl,
}),
});
const data = await response.json();
if (response.ok) {
// Store login status in local storage
localStorage.setItem('isLoggedIn', 'true');
shortenedUrlElement.textContent = `Shortened URL: ${data.shortenedUrl}`;
errorMessageElement.textContent = '';
// Handle the QR code URL from the server response
let qrCodeUrl = data.shortenedUrl;
if (qrCodeUrl && qrCodeUrl !== 'None') {
if (!qrCodeUrl.startsWith('http://') && !qrCodeUrl.startsWith('https://')) {
qrCodeUrl = `http://${qrCodeUrl}`;
}
const qrCodeImageUrl = `${qrCodeUrl}/qrcode`;
qrCodeImage.src = qrCodeImageUrl;
} else {
qrCodeImage.src = '';
}
} else {
errorMessageElement.textContent = data.error || 'An error occurred.';
shortenedUrlElement.textContent = '';
qrCodeImage.src = '';
}
} catch (error) {
errorMessageElement.textContent = 'An error occurred. Please try again later.';
shortenedUrlElement.textContent = '';
qrCodeImage.src = '';
}
});
// Add event listener for the login button (you should customize this logic)
loginButton.addEventListener('click', () => {
// Implement your login logic here
// After successful login, set the user as logged in
localStorage.setItem('isLoggedIn', 'true');
toggleCustomShortUrlField();
});
// Add event listener for the logout button (you should customize this logic)
logoutButton.addEventListener('click', () => {
// Implement your logout logic here
// After logout, set the user as not logged in
localStorage.setItem('isLoggedIn', 'false');
toggleCustomShortUrlField();
});
});
// Function to copy the shortened URL to clipboard
function copyToClipboard() {
const shortenedUrlElement = document.getElementById('shortened-url');
if (shortenedUrlElement) {
const text = shortenedUrlElement.textContent;
if (text) {
const url = text.replace('Shortened URL: ', '').trim();
const textArea = document.createElement('textarea');
textArea.value = url;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
alert('Link copied to clipboard!');
} else {
alert('No link generated.'); // Show error alert when no link is generated
}
}
}