-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
229 lines (194 loc) · 7.61 KB
/
script.js
File metadata and controls
229 lines (194 loc) · 7.61 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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
// =============================================
// LINK LITE – script.js
// Runs on index.html
// =============================================
// ----- GET HTML ELEMENTS -----
const longUrlInput = document.getElementById('longUrl');
const shortenBtn = document.getElementById('shortenBtn');
const resultDiv = document.getElementById('result');
const shortUrlInput = document.getElementById('shortUrl');
const copyBtn = document.getElementById('copyBtn');
const errorMsg = document.getElementById('errorMsg');
const jsonLine = document.getElementById('jsonLine');
const historySection = document.getElementById('historySection');
const historyList = document.getElementById('historyList');
const clearHistoryBtn = document.getElementById('clearHistoryBtn');
// ----- SETTINGS (easy to change) -----
const SHORT_CODE_LENGTH = 6; // how many characters the short code has
// =============================================
// FUNCTION: Generate random short code
// Example output: "k3b9az"
// =============================================
function generateShortCode() {
const characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
let code = '';
for (let i = 0; i < SHORT_CODE_LENGTH; i++) {
const randomIndex = Math.floor(Math.random() * characters.length);
code += characters[randomIndex];
}
return code;
}
// =============================================
// FUNCTION: Validate URL
// Returns true if URL is valid (http or https)
// =============================================
function isValidUrl(string) {
try {
const url = new URL(string);
return url.protocol === 'http:' || url.protocol === 'https:';
} catch (err) {
return false; // URL() throws an error if string is not a valid URL
}
}
// =============================================
// FUNCTION: Show error message in the UI
// Replaces the old alert() popup
// =============================================
function showError(message) {
errorMsg.textContent = message;
errorMsg.classList.remove('hidden');
}
// =============================================
// FUNCTION: Hide error message
// =============================================
function hideError() {
errorMsg.textContent = '';
errorMsg.classList.add('hidden');
}
// =============================================
// FUNCTION: Save a link to localStorage
// localStorage stores data in the browser
// Format: { code: "abc123", url: "https://..." }
// =============================================
function saveToHistory(shortCode, longUrl) {
// Get existing history from localStorage (or empty array if none)
const history = JSON.parse(localStorage.getItem('linkLiteHistory') || '[]');
// Add new entry at the beginning
history.unshift({ code: shortCode, url: longUrl, date: new Date().toLocaleDateString() });
// Keep only last 10 links to avoid overflow
if (history.length > 10) {
history.pop();
}
// Save back to localStorage
localStorage.setItem('linkLiteHistory', JSON.stringify(history));
}
// =============================================
// FUNCTION: Load and display history
// =============================================
function loadHistory() {
const history = JSON.parse(localStorage.getItem('linkLiteHistory') || '[]');
if (history.length === 0) {
historySection.classList.add('hidden');
return;
}
historySection.classList.remove('hidden');
historyList.innerHTML = ''; // Clear old list
history.forEach(function(item) {
const li = document.createElement('li');
li.textContent = `🔗 ${item.code} → ${item.url} (${item.date})`;
historyList.appendChild(li);
});
}
// ----- Load history when page opens -----
loadHistory();
// =============================================
// EVENT: "Shorten" button click
// =============================================
shortenBtn.addEventListener('click', function () {
const longUrl = longUrlInput.value.trim();
// Step 1: Clear any old error
hideError();
// Step 2: Check if input is empty
if (!longUrl) {
showError('⚠️ Please enter a URL first.');
return;
}
// Step 3: Check if URL is valid
if (!isValidUrl(longUrl)) {
showError('⚠️ Please enter a valid URL. It must start with http:// or https://');
return;
}
// Step 4: Generate a short code
const shortCode = generateShortCode();
// Step 5: Build the short URL
// window.location.origin = "https://yourname.github.io"
// window.location.pathname = "/link-lite/index.html"
const basePath = window.location.origin + window.location.pathname.replace('index.html', '');
const shortUrl = `${basePath}redirect.html#${shortCode}`;
// Step 6: Show result in the UI
shortUrlInput.value = shortUrl;
resultDiv.classList.remove('hidden');
// Step 7: Show the JSON line the user needs to add to links.json
jsonLine.textContent = `"${shortCode}": "${longUrl}"`;
// Step 8: Save to localStorage history
saveToHistory(shortCode, longUrl);
loadHistory();
// Step 9: Log to console for debugging
console.log('✅ Short URL created:', shortUrl);
console.log('📝 Add to links.json:', `"${shortCode}": "${longUrl}"`);
});
// =============================================
// EVENT: "Copy" button click
// Uses modern clipboard API (not deprecated)
// =============================================
copyBtn.addEventListener('click', function () {
const textToCopy = shortUrlInput.value;
// navigator.clipboard is the modern, correct way to copy
navigator.clipboard.writeText(textToCopy)
.then(function () {
// Success feedback
copyBtn.textContent = '✓ Copied!';
copyBtn.classList.add('copied');
// Reset after 2 seconds
setTimeout(function () {
copyBtn.textContent = 'Copy';
copyBtn.classList.remove('copied');
}, 2000);
})
.catch(function (err) {
// If clipboard fails (some older browsers), show message
console.error('Copy failed:', err);
alert('Could not copy. Please select the URL manually and press Ctrl+C.');
});
});
// =============================================
// EVENT: Press Enter key to shorten
// =============================================
longUrlInput.addEventListener('keydown', function (e) {
if (e.key === 'Enter') {
shortenBtn.click();
}
});
// =============================================
// EVENT: Clear history button
// =============================================
clearHistoryBtn.addEventListener('click', function () {
localStorage.removeItem('linkLiteHistory');
loadHistory();
});
// =============================================
// DARK MODE TOGGLE
// Saves preference to localStorage
// =============================================
const themeToggle = document.getElementById('themeToggle');
// On page load: apply saved theme
(function applyTheme() {
const saved = localStorage.getItem('linkLiteTheme');
if (saved === 'dark') {
document.body.classList.add('dark');
if (themeToggle) themeToggle.textContent = '☀️ Light Mode';
}
})();
// On button click: toggle theme
if (themeToggle) {
themeToggle.addEventListener('click', function () {
const isDark = document.body.classList.toggle('dark');
if (isDark) {
themeToggle.textContent = '☀️ Light Mode';
localStorage.setItem('linkLiteTheme', 'dark');
} else {
themeToggle.textContent = '🌙 Dark Mode';
localStorage.setItem('linkLiteTheme', 'light');
}
});
}