|
| 1 | +const grid = document.getElementById('gameGrid'); |
| 2 | +const overlay = document.getElementById('viewerOverlay'); |
| 3 | +const frame = document.getElementById('viewerFrame'); |
| 4 | +const closeBtn = document.getElementById('closeBtn'); |
| 5 | +const newTabBtn = document.getElementById('newTabBtn'); |
| 6 | +const status = document.getElementById('status'); |
| 7 | +const searchBox = document.getElementById('searchBox'); |
| 8 | +const gameCountEl = document.getElementById('gameCount'); |
| 9 | +const viewerTitle = document.getElementById('viewerTitle'); |
| 10 | + |
| 11 | +const HTML_BASE_URL = 'https://cdn.jsdelivr.net/gh/gn-math/html@master'; |
| 12 | + |
| 13 | +let games = []; |
| 14 | +let filteredGames = []; |
| 15 | +let currentGameUrl = ''; |
| 16 | + |
| 17 | +const imageObserver = new IntersectionObserver((entries, observer) => { |
| 18 | + entries.forEach(entry => { |
| 19 | + if (entry.isIntersecting) { |
| 20 | + const img = entry.target; |
| 21 | + img.src = img.dataset.src; |
| 22 | + observer.unobserve(img); |
| 23 | + } |
| 24 | + }); |
| 25 | +}, { |
| 26 | + rootMargin: '200px' |
| 27 | +}); |
| 28 | + |
| 29 | +function createCardElement(game) { |
| 30 | + const card = document.createElement('div'); |
| 31 | + card.className = 'card'; |
| 32 | + |
| 33 | + const img = document.createElement('img'); |
| 34 | + img.loading = 'lazy'; |
| 35 | + img.dataset.src = game.cover.replace('{COVER_URL}', 'https://cdn.jsdelivr.net/gh/gn-math/covers@main'); |
| 36 | + |
| 37 | + imageObserver.observe(img); |
| 38 | + |
| 39 | + const title = document.createElement('h3'); |
| 40 | + title.textContent = game.name; |
| 41 | + |
| 42 | + card.appendChild(img); |
| 43 | + card.appendChild(title); |
| 44 | + |
| 45 | + card.onclick = () => openGame(game.url, game.name); |
| 46 | + |
| 47 | + return card; |
| 48 | +} |
| 49 | + |
| 50 | +async function loadGames() { |
| 51 | + status.style.display = 'block'; |
| 52 | + status.textContent = "Loading games..."; |
| 53 | + try { |
| 54 | + const res = await fetch('https://cdn.jsdelivr.net/gh/HamCraft-UB/HamCraft-UB-jsdeliver-assets@main/apps/apps.json', { cache: "no-store" }); |
| 55 | + const data = await res.json(); |
| 56 | + |
| 57 | + games = data |
| 58 | + .filter(g => g.id >= 0 && !g.name.includes('[!]')) |
| 59 | + .map(g => ({ |
| 60 | + ...g, |
| 61 | + url: g.url.replace('{HTML_URL}', HTML_BASE_URL) |
| 62 | + })); |
| 63 | + |
| 64 | + filteredGames = [...games]; |
| 65 | + |
| 66 | + grid.innerHTML = ""; |
| 67 | + filteredGames.forEach(game => { |
| 68 | + grid.appendChild(createCardElement(game)); |
| 69 | + }); |
| 70 | + |
| 71 | + gameCountEl.textContent = `Games: ${games.length}`; |
| 72 | + status.style.display = 'none'; |
| 73 | + } catch (e) { |
| 74 | + status.textContent = "ERROR loading games."; |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +searchBox.addEventListener('input', () => { |
| 79 | + const q = searchBox.value.toLowerCase().trim(); |
| 80 | + filteredGames = games.filter(g => g.name.toLowerCase().includes(q)); |
| 81 | + |
| 82 | + grid.innerHTML = ""; |
| 83 | + filteredGames.forEach(game => { |
| 84 | + grid.appendChild(createCardElement(game)); |
| 85 | + }); |
| 86 | +}); |
| 87 | + |
| 88 | +async function openGame(url, name) { |
| 89 | + overlay.style.display = 'flex'; |
| 90 | + viewerTitle.textContent = name || url; |
| 91 | + currentGameUrl = url; |
| 92 | + |
| 93 | + try { |
| 94 | + const res = await fetch(url); |
| 95 | + const html = await res.text(); |
| 96 | + const doc = frame.contentWindow.document; |
| 97 | + doc.open(); |
| 98 | + doc.write(html); |
| 99 | + doc.close(); |
| 100 | + } catch (err) { |
| 101 | + console.error(err); |
| 102 | + frame.src = 'about:blank'; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +closeBtn.onclick = () => { |
| 107 | + overlay.style.display = 'none'; |
| 108 | + frame.src = 'about:blank'; |
| 109 | + currentGameUrl = ''; |
| 110 | +}; |
| 111 | + |
| 112 | +newTabBtn.onclick = async () => { |
| 113 | + if (!currentGameUrl) return; |
| 114 | + |
| 115 | + try { |
| 116 | + const res = await fetch(currentGameUrl); |
| 117 | + const html = await res.text(); |
| 118 | + |
| 119 | + const win = window.open('about:blank', '_blank'); |
| 120 | + if (!win) return; |
| 121 | + |
| 122 | + win.document.open(); |
| 123 | + win.document.write(html); |
| 124 | + win.document.close(); |
| 125 | + } catch (err) { |
| 126 | + console.error('Failed to open in new tab:', err); |
| 127 | + } |
| 128 | +}; |
| 129 | + |
| 130 | +loadGames(); |
0 commit comments