-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
238 lines (198 loc) · 7.43 KB
/
script.js
File metadata and controls
238 lines (198 loc) · 7.43 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
230
231
232
233
234
235
236
237
238
// Helper function to get URL from redirect entry
function getRedirectUrl(entry) {
return typeof entry === 'string' ? entry : entry.url;
}
// Helper function to get description from redirect entry
function getRedirectDescription(entry) {
return typeof entry === 'string' ? '' : (entry.description || '');
}
// Global variables for search
let allLinks = [];
let currentSearchTerm = '';
// Main application functions
function getCurrentDomain() {
const hostname = window.location.hostname;
if (hostname.includes('github.io')) {
return hostname + '/links';
}
return hostname;
}
function getShortlinkUrl(shortcut) {
const hostname = window.location.hostname;
const protocol = window.location.protocol;
if (hostname.includes('github.io')) {
return `${protocol}//${hostname}/links/${shortcut}`;
}
return `${protocol}//${hostname}/${shortcut}`;
}
function showToast(message) {
// Remove existing toast if any
const existingToast = document.querySelector('.toast');
if (existingToast) {
existingToast.remove();
}
// Create new toast
const toast = document.createElement('div');
toast.className = 'toast';
toast.textContent = message;
document.body.appendChild(toast);
// Show toast
setTimeout(() => toast.classList.add('show'), 100);
// Hide and remove toast
setTimeout(() => {
toast.classList.remove('show');
setTimeout(() => toast.remove(), 300);
}, 2000);
}
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
showToast('Link copied to clipboard!');
}).catch(() => {
// Fallback for browsers that don't support clipboard API
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
showToast('Link copied to clipboard!');
});
}
function getDomainFromUrl(url) {
try {
return new URL(url).hostname;
} catch {
return url;
}
}
function getSimpleUrl(url) {
try {
const parsed = new URL(url);
return parsed.hostname + parsed.pathname;
} catch {
return url;
}
}
function getFaviconUrl(url) {
try {
const domain = new URL(url).origin;
return `https://www.google.com/s2/favicons?domain=${domain}&sz=32`;
} catch {
return null;
}
}
function createLinkCard(shortcut, redirectEntry, searchTerm = '') {
const destinationUrl = getRedirectUrl(redirectEntry);
const description = getRedirectDescription(redirectEntry) || '';
const shortlinkUrl = getShortlinkUrl(shortcut);
const destinationDomain = getDomainFromUrl(destinationUrl);
const faviconUrl = getFaviconUrl(destinationUrl);
const displayShortcut = highlightMatch(shortcut, searchTerm);
const displayDescription = highlightMatch(description, searchTerm);
const displayUrl = highlightMatch(getSimpleUrl(destinationUrl), searchTerm);
const displayDomain = highlightMatch(destinationDomain, searchTerm);
return `
<article title="${destinationUrl}"
data-shortcut="${shortcut}"
data-description="${description.toLowerCase()}"
data-url="${destinationUrl.toLowerCase()}"
data-domain="${destinationDomain.toLowerCase()}">
<header>
${faviconUrl ? `<img src="${faviconUrl}" alt="" class="favicon" onerror="this.style.display='none'">` : ''}
/${displayShortcut}
<br>
<small>→ ${displayUrl}</small>
</header>
${description ? `<div class="link-description">${displayDescription}</div>` : ''}
<footer>
<button onclick="copyToClipboard('${shortlinkUrl}')" title="Copy short link">
📋 Copy
</button>
<button class="secondary" onclick="window.open('${shortlinkUrl}', '_blank')" title="Visit ${destinationUrl}">
🔗 Visit
</button>
</footer>
</article>
`;
}
function highlightMatch(text, searchTerm) {
if (!searchTerm) return text;
const regex = new RegExp(`(${searchTerm})`, 'gi');
return text.replace(regex, '<mark>$1</mark>');
}
function filterLinks(searchTerm) {
const container = document.getElementById('links-container');
const noResults = document.getElementById('no-results');
const searchResultsCount = document.getElementById('search-results-count');
if (!searchTerm.trim()) {
// Show all links
const linkCards = allLinks.map(([shortcut, redirectEntry]) =>
createLinkCard(shortcut, redirectEntry)
).join('');
container.innerHTML = linkCards;
noResults.style.display = 'none';
container.style.display = 'grid';
searchResultsCount.textContent = '';
return;
}
// Filter links
const filteredLinks = allLinks.filter(([shortcut, redirectEntry]) => {
const destinationUrl = getRedirectUrl(redirectEntry);
const description = getRedirectDescription(redirectEntry);
const destinationDomain = getDomainFromUrl(destinationUrl);
const searchLower = searchTerm.toLowerCase();
return (
shortcut.toLowerCase().includes(searchLower) ||
description.toLowerCase().includes(searchLower) ||
destinationUrl.toLowerCase().includes(searchLower) ||
destinationDomain.toLowerCase().includes(searchLower)
);
});
if (filteredLinks.length === 0) {
container.style.display = 'none';
noResults.style.display = 'block';
searchResultsCount.textContent = 'No results found';
} else {
const linkCards = filteredLinks.map(([shortcut, redirectEntry]) =>
createLinkCard(shortcut, redirectEntry, searchTerm)
).join('');
container.innerHTML = linkCards;
container.style.display = 'grid';
noResults.style.display = 'none';
const resultText = filteredLinks.length === 1 ? 'result' : 'results';
searchResultsCount.textContent = `${filteredLinks.length} ${resultText} found`;
}
}
function setupSearch() {
const searchInput = document.getElementById('search-input');
// Real-time search as user types
searchInput.addEventListener('input', (e) => {
currentSearchTerm = e.target.value;
filterLinks(currentSearchTerm);
});
// Handle Enter key
searchInput.addEventListener('keydown', (e) => {
if (e.key === 'Enter') {
e.preventDefault();
// Maybe focus first result or do something else
}
});
// Handle Escape key to clear search
searchInput.addEventListener('keydown', (e) => {
if (e.key === 'Escape') {
searchInput.value = '';
currentSearchTerm = '';
filterLinks('');
}
});
}
function renderLinks() {
// Store all links for searching
allLinks = Object.entries(REDIRECTS).sort(([a], [b]) => a.localeCompare(b));
// Initial render of all links
filterLinks('');
// Setup search functionality
setupSearch();
}
// Initialize page when DOM is loaded
document.addEventListener('DOMContentLoaded', renderLinks);