|
| 1 | +// GitHub Latest Release Info Script |
| 2 | +// Usage: |
| 3 | +// 1. Add <div id="github-latest-release"></div> where you want the info. |
| 4 | +// 2. Add <script src="/scripts/github-latest-release.js"></script> to your page. |
| 5 | + |
| 6 | +const GITHUB_OWNER = 'usnistgov'; |
| 7 | +const GITHUB_REPO = 'macos_security'; |
| 8 | +const CONTAINER_ID = 'github-latest-release'; |
| 9 | + |
| 10 | +function renderReleaseInfo({ tag_name, name, html_url, published_at, body }) { |
| 11 | + const date = published_at |
| 12 | + ? new Date(published_at).toLocaleDateString(undefined, { year: 'numeric', month: 'short', day: 'numeric' }) |
| 13 | + : ''; |
| 14 | + |
| 15 | + let notes = ''; |
| 16 | + if (body) { |
| 17 | + notes = body.replace(/</g, "<"); |
| 18 | + notes = notes.replace( |
| 19 | + /\*\*Full Changelog\*\*:\s*(https?:\/\/[^\s]+)/g, |
| 20 | + (match, url) => |
| 21 | + `<a href="${url}" target="_blank" rel="noopener">Full Changelog</a>` |
| 22 | + ); |
| 23 | + notes = `<div class="github-release-notes" style="margin-top:1em; font-size:0.98em; color:var(--sl-color-text, #444); white-space:pre-line;">${notes}</div>`; |
| 24 | + } |
| 25 | + return ` |
| 26 | + <div class="github-release-info"> |
| 27 | + <strong><a href="${html_url}" target="_blank" rel="noopener"> |
| 28 | + ${name || tag_name} |
| 29 | + </a></strong> |
| 30 | + <span style="color: #888; font-size: 0.9em; font-style: italic;">(${tag_name})</span> |
| 31 | + ${date ? `<div style="font-size:0.9em; color:var(--sl-color-text, #666); margin-top:2px;">Released: <strong>${date}</strong></div>` : ''} |
| 32 | + ${notes} |
| 33 | + <div style="margin-top:0.7em;"> |
| 34 | + <a href="https://github.com/${GITHUB_OWNER}/${GITHUB_REPO}/releases" target="_blank" rel="noopener" style="font-size:0.95em;"> |
| 35 | + View all releases → |
| 36 | + </a> |
| 37 | + </div> |
| 38 | + </div> |
| 39 | + `; |
| 40 | +} |
| 41 | + |
| 42 | +function showReleaseLoading() { |
| 43 | + const container = document.getElementById(CONTAINER_ID); |
| 44 | + if (container) container.innerHTML = 'Loading latest release...'; |
| 45 | +} |
| 46 | + |
| 47 | +function showReleaseError() { |
| 48 | + const container = document.getElementById(CONTAINER_ID); |
| 49 | + if (container) container.innerHTML = 'Could not load release info.'; |
| 50 | +} |
| 51 | + |
| 52 | +function fetchLatestRelease() { |
| 53 | + showReleaseLoading(); |
| 54 | + fetch(`https://api.github.com/repos/${GITHUB_OWNER}/${GITHUB_REPO}/releases/latest`) |
| 55 | + .then(res => { |
| 56 | + if (!res.ok) throw new Error('Network response was not ok'); |
| 57 | + return res.json(); |
| 58 | + }) |
| 59 | + .then(release => { |
| 60 | + const container = document.getElementById(CONTAINER_ID); |
| 61 | + if (container) container.innerHTML = renderReleaseInfo(release); |
| 62 | + }) |
| 63 | + .catch(() => showReleaseError()); |
| 64 | +} |
| 65 | + |
| 66 | +function injectReleaseBoxStyles() { |
| 67 | + if (document.getElementById('github-release-style')) return; |
| 68 | + const style = document.createElement('style'); |
| 69 | + style.id = 'github-release-style'; |
| 70 | + style.textContent = ` |
| 71 | + .github-release-info { |
| 72 | + border-radius: 12px; |
| 73 | + padding: 1.25em 1.5em; |
| 74 | + margin: 1.5em 0; |
| 75 | + border: 1px solid #cbd3d8ff; |
| 76 | + background: var(--sl-color-bg, #fff); |
| 77 | + color: var(--sl-color-text, #316431); |
| 78 | + box-shadow: 0 2px 8px 0 rgba(60,60,60,0.06); |
| 79 | + transition: background 0.2s, color 0.2s, border-color 0.2s; |
| 80 | + } |
| 81 | + [data-theme="dark"] .github-release-info { |
| 82 | + background: var(--sl-color-bg, #161b22); |
| 83 | + color: var(--sl-color-text, #6ab549); |
| 84 | + border-color: #2d3133ff; |
| 85 | + box-shadow: 0 2px 8px 0 rgba(0,0,0,0.10); |
| 86 | + } |
| 87 | + .github-release-info a { |
| 88 | + color: var(--sl-color-accent, #316431); |
| 89 | + text-decoration: none; |
| 90 | + font-weight: 500; |
| 91 | + transition: color 0.2s; |
| 92 | + } |
| 93 | + [data-theme="dark"] .github-release-info a { |
| 94 | + color: var(--sl-color-accent-high, #6ab549); |
| 95 | + } |
| 96 | + .github-release-info a:hover { |
| 97 | + text-decoration: underline; |
| 98 | + } |
| 99 | + .github-release-info span { |
| 100 | + color: #888; |
| 101 | + font-size: 0.9em; |
| 102 | + } |
| 103 | + [data-theme="dark"] .github-release-info span { |
| 104 | + color: #aaa; |
| 105 | + } |
| 106 | + .github-release-info strong { |
| 107 | + color: #111; |
| 108 | + font-size: 1.08em; |
| 109 | + } |
| 110 | + [data-theme="dark"] .github-release-info strong { |
| 111 | + color: #fff; |
| 112 | + } |
| 113 | + `; |
| 114 | + document.head.appendChild(style); |
| 115 | +} |
| 116 | + |
| 117 | +window.addEventListener('DOMContentLoaded', () => { |
| 118 | + injectReleaseBoxStyles(); |
| 119 | + fetchLatestRelease(); |
| 120 | +}); |
0 commit comments