Spaces:
Paused
Paused
File size: 10,360 Bytes
d22875e | 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | /**
* RandomWeb β Frontend Application Logic
* Handles random redirect, search, submission, and real-time counter.
*/
// βββ Configuration ββββββββββββββββββββββββββββββββββββββββββ
const SUPABASE_URL = 'https://oyxgydfmaocqxictnmou.supabase.co';
const SUPABASE_KEY = 'sb_publishable_9l3BSqU-mIdYLEgZB2Pv2Q_UUZXU385';
const API_BASE = '/api';
// βββ Supabase Client ββββββββββββββββββββββββββββββββββββββββ
const supabase = window.supabase.createClient(SUPABASE_URL, SUPABASE_KEY);
// βββ DOM Elements βββββββββββββββββββββββββββββββββββββββββββ
const randomBtn = document.getElementById('random-btn');
const btnText = randomBtn.querySelector('.btn-text');
const searchInput = document.getElementById('search-input');
const searchResults = document.getElementById('search-results');
const submitForm = document.getElementById('submit-form');
const submitInput = document.getElementById('submit-input');
const submitBtn = document.getElementById('submit-btn');
const submitFeedback = document.getElementById('submit-feedback');
const counterValue = document.getElementById('counter-value');
const headerActiveCount = document.getElementById('header-active-count');
const toastContainer = document.getElementById('toast-container');
// βββ State ββββββββββββββββββββββββββββββββββββββββββββββββββ
let currentCount = 0;
let targetCount = 0;
let animationFrame = null;
let searchDebounceTimer = null;
// βββ Utility Functions ββββββββββββββββββββββββββββββββββββββ
function formatNumber(num) {
if (num >= 1_000_000) {
return (num / 1_000_000).toFixed(2) + 'M';
}
if (num >= 1_000) {
return (num / 1_000).toFixed(1) + 'K';
}
return num.toLocaleString();
}
function formatNumberFull(num) {
return num.toLocaleString();
}
function showToast(message, type = 'info') {
const toast = document.createElement('div');
toast.className = `toast toast-${type}`;
toast.textContent = message;
toastContainer.appendChild(toast);
setTimeout(() => {
toast.classList.add('toast-exiting');
setTimeout(() => toast.remove(), 300);
}, 4000);
}
// βββ Animated Counter βββββββββββββββββββββββββββββββββββββββ
function animateCounter(target) {
targetCount = target;
if (animationFrame) {
cancelAnimationFrame(animationFrame);
}
const startCount = currentCount;
const diff = target - startCount;
const duration = Math.min(1500, Math.max(300, Math.abs(diff) * 10));
const startTime = performance.now();
function step(timestamp) {
const elapsed = timestamp - startTime;
const progress = Math.min(elapsed / duration, 1);
// Ease-out cubic
const eased = 1 - Math.pow(1 - progress, 3);
currentCount = Math.round(startCount + diff * eased);
counterValue.textContent = formatNumberFull(currentCount);
headerActiveCount.textContent = formatNumber(currentCount);
if (progress < 1) {
animationFrame = requestAnimationFrame(step);
} else {
currentCount = target;
counterValue.textContent = formatNumberFull(target);
headerActiveCount.textContent = formatNumber(target);
}
}
animationFrame = requestAnimationFrame(step);
}
// βββ Fetch Stats (Initial) βββββββββββββββββββββββββββββββββ
async function fetchStats() {
try {
const response = await fetch(`${API_BASE}/stats`);
if (response.ok) {
const data = await response.json();
animateCounter(data.active_count);
}
} catch (err) {
console.warn('Failed to fetch stats:', err);
// Fallback: query Supabase directly
try {
const { data, error } = await supabase
.from('stats')
.select('active_count')
.eq('id', 1)
.single();
if (!error && data) {
animateCounter(data.active_count);
}
} catch (e) {
console.warn('Supabase fallback also failed:', e);
}
}
}
// βββ Realtime Subscription ββββββββββββββββββββββββββββββββββ
function setupRealtimeSubscription() {
const channel = supabase
.channel('stats-realtime')
.on(
'postgres_changes',
{
event: 'UPDATE',
schema: 'public',
table: 'stats',
filter: 'id=eq.1',
},
(payload) => {
const newCount = payload.new.active_count;
if (newCount !== undefined && newCount !== targetCount) {
animateCounter(newCount);
}
}
)
.subscribe((status) => {
if (status === 'SUBSCRIBED') {
console.log('Realtime subscription active');
}
});
}
// Also poll every 30 seconds as a fallback
setInterval(fetchStats, 30000);
// βββ Random Button ββββββββββββββββββββββββββββββββββββββββββ
randomBtn.addEventListener('click', async () => {
if (randomBtn.classList.contains('loading')) return;
randomBtn.classList.add('loading');
btnText.textContent = 'Finding a website...';
try {
const response = await fetch(`${API_BASE}/random`);
if (response.ok) {
const data = await response.json();
if (data.url) {
btnText.textContent = 'Redirecting...';
// Small delay for visual feedback
setTimeout(() => {
window.open(data.url, '_blank', 'noopener,noreferrer');
randomBtn.classList.remove('loading');
btnText.textContent = 'Take Me Somewhere Random';
}, 500);
return;
}
}
// API failed, try direct Supabase query
const { data: websites, error } = await supabase
.rpc('get_random_active_website');
if (!error && websites && websites.length > 0) {
btnText.textContent = 'Redirecting...';
setTimeout(() => {
window.open(websites[0].url, '_blank', 'noopener,noreferrer');
randomBtn.classList.remove('loading');
btnText.textContent = 'Take Me Somewhere Random';
}, 500);
return;
}
showToast('No active websites found yet. The system is still indexing.', 'info');
} catch (err) {
console.error('Random fetch error:', err);
showToast('Failed to get a random website. Please try again.', 'error');
}
randomBtn.classList.remove('loading');
btnText.textContent = 'Take Me Somewhere Random';
});
// βββ Search βββββββββββββββββββββββββββββββββββββββββββββββββ
searchInput.addEventListener('input', (e) => {
const query = e.target.value.trim();
clearTimeout(searchDebounceTimer);
if (query.length < 2) {
searchResults.innerHTML = '';
return;
}
searchDebounceTimer = setTimeout(() => performSearch(query), 300);
});
async function performSearch(query) {
try {
const response = await fetch(
`${API_BASE}/search?q=${encodeURIComponent(query)}&limit=15`
);
if (response.ok) {
const results = await response.json();
renderSearchResults(results);
return;
}
// Fallback to direct Supabase
const { data, error } = await supabase
.from('websites')
.select('url, domain, is_active')
.or(`url.ilike.%${query}%,domain.ilike.%${query}%`)
.eq('is_active', true)
.limit(15);
if (!error && data) {
renderSearchResults(data);
}
} catch (err) {
console.error('Search error:', err);
}
}
function renderSearchResults(results) {
if (!results || results.length === 0) {
searchResults.innerHTML = `
<div class="search-empty">
No matching websites found. Try a different search term.
</div>
`;
return;
}
searchResults.innerHTML = results
.map(
(r) => `
<a href="${escapeHtml(r.url)}" target="_blank" rel="noopener noreferrer"
class="search-result-item">
<div>
<div class="result-url">${escapeHtml(r.url)}</div>
<div class="result-domain">${escapeHtml(r.domain)}</div>
</div>
<span class="result-arrow">β</span>
</a>
`
)
.join('');
}
function escapeHtml(text) {
const div = document.createElement('div');
div.textContent = text;
return div.innerHTML;
}
// βββ Submit Form ββββββββββββββββββββββββββββββββββββββββββββ
submitForm.addEventListener('submit', async (e) => {
e.preventDefault();
const url = submitInput.value.trim();
if (!url) return;
submitBtn.disabled = true;
submitBtn.textContent = 'Submitting...';
submitFeedback.className = 'submit-feedback';
submitFeedback.style.display = 'none';
try {
const response = await fetch(`${API_BASE}/submit`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ url }),
});
const data = await response.json();
if (response.ok) {
submitFeedback.className = 'submit-feedback success';
submitFeedback.textContent = data.message || 'URL submitted successfully!';
submitInput.value = '';
} else {
submitFeedback.className = 'submit-feedback error';
submitFeedback.textContent =
data.detail || 'Failed to submit URL. Please check the format.';
}
} catch (err) {
submitFeedback.className = 'submit-feedback error';
submitFeedback.textContent = 'Network error. Please try again.';
}
submitBtn.disabled = false;
submitBtn.textContent = 'Submit URL';
});
// βββ Initialize βββββββββββββββββββββββββββββββββββββββββββββ
document.addEventListener('DOMContentLoaded', () => {
fetchStats();
setupRealtimeSubscription();
});
|