|
| 1 | +(function () { |
| 2 | + var API_URL = |
| 3 | + "https://api.github.com/repos/ghostunnel/ghostunnel/releases?per_page=100"; |
| 4 | + |
| 5 | + var OS_LABELS = { |
| 6 | + linux: "Linux", |
| 7 | + darwin: "macOS", |
| 8 | + windows: "Windows", |
| 9 | + }; |
| 10 | + |
| 11 | + var ARCH_LABELS = { |
| 12 | + amd64: "x86_64", |
| 13 | + arm64: "ARM64", |
| 14 | + universal: "Universal", |
| 15 | + }; |
| 16 | + |
| 17 | + function parseAssetName(name) { |
| 18 | + // Expected: ghostunnel-{os}-{arch}[.exe] |
| 19 | + var m = name.match(/^ghostunnel-(\w+)-(\w+?)(\.exe)?$/); |
| 20 | + if (!m) return null; |
| 21 | + return { os: m[1], arch: m[2], exe: !!m[3] }; |
| 22 | + } |
| 23 | + |
| 24 | + function renderDownloads(container, assets) { |
| 25 | + // Group by OS |
| 26 | + var byOS = {}; |
| 27 | + assets.forEach(function (a) { |
| 28 | + var info = parseAssetName(a.name); |
| 29 | + if (!info) return; |
| 30 | + if (!byOS[info.os]) byOS[info.os] = []; |
| 31 | + byOS[info.os].push({ info: info, url: a.browser_download_url, name: a.name, size: a.size }); |
| 32 | + }); |
| 33 | + |
| 34 | + var osOrder = ["linux", "darwin", "windows"]; |
| 35 | + var items = []; |
| 36 | + |
| 37 | + osOrder.forEach(function (os) { |
| 38 | + if (!byOS[os]) return; |
| 39 | + byOS[os].forEach(function (asset) { |
| 40 | + var label = OS_LABELS[asset.info.os] || asset.info.os; |
| 41 | + var arch = ARCH_LABELS[asset.info.arch] || asset.info.arch; |
| 42 | + var sizeMB = (asset.size / (1024 * 1024)).toFixed(1); |
| 43 | + items.push( |
| 44 | + '<a class="download-link" href="' + asset.url + '" title="' + asset.name + '">' + |
| 45 | + '<span class="download-os">' + label + '</span> ' + |
| 46 | + '<span class="download-arch">' + arch + '</span>' + |
| 47 | + '<span class="download-size">' + sizeMB + ' MB</span>' + |
| 48 | + "</a>" |
| 49 | + ); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + if (items.length === 0) return; |
| 54 | + |
| 55 | + container.innerHTML = |
| 56 | + '<div class="download-bar">' + |
| 57 | + '<span class="download-label">Downloads</span>' + |
| 58 | + '<span class="download-items">' + items.join("") + '</span>' + |
| 59 | + "</div>"; |
| 60 | + } |
| 61 | + |
| 62 | + function populate(releases) { |
| 63 | + var tagMap = {}; |
| 64 | + releases.forEach(function (r) { |
| 65 | + if (r.assets && r.assets.length > 0) { |
| 66 | + tagMap[r.tag_name] = r.assets; |
| 67 | + } |
| 68 | + }); |
| 69 | + |
| 70 | + var containers = document.querySelectorAll(".release-downloads[data-tag]"); |
| 71 | + containers.forEach(function (el) { |
| 72 | + var tag = el.getAttribute("data-tag"); |
| 73 | + if (tagMap[tag]) { |
| 74 | + renderDownloads(el, tagMap[tag]); |
| 75 | + } |
| 76 | + }); |
| 77 | + } |
| 78 | + |
| 79 | + // Fetch all releases (up to 100, which covers all current releases) |
| 80 | + fetch(API_URL) |
| 81 | + .then(function (res) { |
| 82 | + if (!res.ok) return []; |
| 83 | + return res.json(); |
| 84 | + }) |
| 85 | + .then(populate) |
| 86 | + .catch(function () { |
| 87 | + // Silently fail — GitHub/Docker links in the header are the fallback |
| 88 | + }); |
| 89 | +})(); |
0 commit comments