From 63dd7234d62881cc2d8b01eaaabfef7cceafc923 Mon Sep 17 00:00:00 2001 From: cogwheel0 <172976095+cogwheel0@users.noreply.github.com> Date: Wed, 26 Nov 2025 23:39:14 +0530 Subject: [PATCH] refactor(stats): Update GitHub stats fetching to use data attributes and improve API request handling --- docs/index.html | 4 +- docs/script.js | 98 +++++++++++++++++++++++++++++-------------------- 2 files changed, 61 insertions(+), 41 deletions(-) diff --git a/docs/index.html b/docs/index.html index a5d9215..9a3dbfb 100644 --- a/docs/index.html +++ b/docs/index.html @@ -65,7 +65,7 @@ - + Stars @@ -74,7 +74,7 @@ - + Downloads diff --git a/docs/script.js b/docs/script.js index cc5c917..7829494 100644 --- a/docs/script.js +++ b/docs/script.js @@ -273,9 +273,9 @@ document.addEventListener('DOMContentLoaded', () => { }); // ============================================ - // FETCH GITHUB STATS + // FETCH REPOSITORY STATS // ============================================ - const formatNumber = (num) => { + const formatCount = (num) => { if (num >= 1000000) { return (num / 1000000).toFixed(1).replace(/\.0$/, '') + 'M'; } @@ -285,48 +285,68 @@ document.addEventListener('DOMContentLoaded', () => { return num.toString(); }; - const starsElement = document.getElementById('github-stars'); - const downloadsElement = document.getElementById('github-downloads'); + // Use data attributes instead of IDs to avoid ad blocker element hiding + const starsEl = document.querySelector('[data-metric="stars"]'); + const downloadsEl = document.querySelector('[data-metric="downloads"]'); - // Fetch repo stats (stars) - if (starsElement) { - fetch('https://api.github.com/repos/cogwheel0/conduit') - .then(response => response.json()) - .then(data => { - if (data.stargazers_count !== undefined) { - starsElement.textContent = formatNumber(data.stargazers_count); - starsElement.classList.add('loaded'); - } - }) - .catch(() => { - starsElement.textContent = '★'; - }); + // Build API URL dynamically to avoid static pattern matching + const apiBase = ['https:/', '/api', '.github', '.com/'].join(''); + const repoPath = 'repos/cogwheel0/conduit'; + + // Fetch repo stats using a class-based approach (less likely to be blocked) + const fetchRepoData = async () => { + try { + const endpoint = apiBase + repoPath; + const response = await fetch(endpoint); + if (!response.ok) throw new Error('API request failed'); + return await response.json(); + } catch (e) { + return null; + } + }; + + const fetchReleaseData = async () => { + try { + const endpoint = apiBase + repoPath + '/releases'; + const response = await fetch(endpoint); + if (!response.ok) throw new Error('API request failed'); + return await response.json(); + } catch (e) { + return null; + } + }; + + // Initialize stats + if (starsEl) { + fetchRepoData().then(data => { + if (data && data.stargazers_count !== undefined) { + starsEl.textContent = formatCount(data.stargazers_count); + starsEl.classList.add('loaded'); + } else { + starsEl.textContent = '★'; + } + }); } - // Fetch releases (downloads) - if (downloadsElement) { - fetch('https://api.github.com/repos/cogwheel0/conduit/releases') - .then(response => response.json()) - .then(releases => { - if (Array.isArray(releases)) { - const totalDownloads = releases.reduce((total, release) => { - return total + release.assets.reduce((assetTotal, asset) => { - return assetTotal + (asset.download_count || 0); - }, 0); + if (downloadsEl) { + fetchReleaseData().then(releases => { + if (Array.isArray(releases)) { + const total = releases.reduce((sum, release) => { + return sum + release.assets.reduce((assetSum, asset) => { + return assetSum + (asset.download_count || 0); }, 0); - - if (totalDownloads > 0) { - downloadsElement.textContent = formatNumber(totalDownloads); - } else { - // If no GitHub downloads, show a generic indicator - downloadsElement.textContent = 'New'; - } - downloadsElement.classList.add('loaded'); + }, 0); + + if (total > 0) { + downloadsEl.textContent = formatCount(total); + } else { + downloadsEl.textContent = 'New'; } - }) - .catch(() => { - downloadsElement.textContent = '↓'; - }); + downloadsEl.classList.add('loaded'); + } else { + downloadsEl.textContent = '↓'; + } + }); } console.log('✨ Conduit landing page initialized');