11// GitHub Stats Fetcher
2- // Automatically fetches star and fork counts from GitHub API
2+ // Loads stats only when open-source section is clicked
33
44document . addEventListener ( 'DOMContentLoaded' , function ( ) {
55 // GitHub API endpoint (no authentication required for public repos)
@@ -15,11 +15,42 @@ document.addEventListener('DOMContentLoaded', function() {
1515 { name : 'moatlab/IODA-SOSP21-AE' , elementId : 'ioda-stats' }
1616 ] ;
1717
18- // Fetch stats for each repository
19- repos . forEach ( repo => {
20- fetchRepoStats ( repo . name , repo . elementId ) ;
18+ let statsLoaded = false ;
19+
20+ // Function to load GitHub stats
21+ function loadGitHubStats ( ) {
22+ if ( statsLoaded ) return ; // Only load once
23+
24+ console . log ( 'Loading GitHub stats...' ) ;
25+ statsLoaded = true ;
26+
27+ // Fetch stats for each repository
28+ repos . forEach ( repo => {
29+ fetchRepoStats ( repo . name , repo . elementId ) ;
30+ } ) ;
31+ }
32+
33+ // Listen for clicks on the open-source section
34+ document . addEventListener ( 'click' , function ( event ) {
35+ // Check if the click is on or within the open-source section
36+ const target = event . target ;
37+ const openSourceSection = target . closest ( '.github-repos' ) ||
38+ target . closest ( '[href*="opensource"]' ) ||
39+ target . closest ( '[href*="open-source"]' ) ;
40+
41+ if ( openSourceSection ) {
42+ // Small delay to ensure the section is fully loaded
43+ setTimeout ( loadGitHubStats , 100 ) ;
44+ }
2145 } ) ;
2246
47+ // Also listen for navigation to the open-source page
48+ if ( window . location . pathname . includes ( 'opensource' ) ||
49+ window . location . pathname . includes ( 'open-source' ) ) {
50+ // If we're already on the open-source page, load stats after a short delay
51+ setTimeout ( loadGitHubStats , 500 ) ;
52+ }
53+
2354 async function fetchRepoStats ( repoName , elementId ) {
2455 try {
2556 const response = await fetch ( `${ GITHUB_API_BASE } /${ repoName } ` ) ;
@@ -40,20 +71,35 @@ document.addEventListener('DOMContentLoaded', function() {
4071 function updateStatsDisplay ( elementId , stars , forks ) {
4172 const element = document . getElementById ( elementId ) ;
4273 if ( element ) {
43- element . innerHTML = `
44- <span class="repo-stars">
45- <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
46- <path d="M8 .25a.75.75 0 0 1 .673.418l3.058 6.197 6.839.994a.75.75 0 0 1 .415 1.279l-4.948 4.823 1.168 6.811a.75.75 0 0 1-1.088.791L8 14.347l-6.116 3.216a.75.75 0 0 1-1.088-.79l1.168-6.812-4.948-4.823a.75.75 0 0 1 .415-1.28l6.838-.993L7.327.668A.75.75 0 0 1 8 .25Z"/>
47- </svg>
48- ${ stars }
49- </span>
50- <span class="repo-forks">
51- <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
52- <path d="M5 5.372v.878c0 .414.336.75.75.75h4.5a.75.75 0 0 0 .75-.75v-.878a2.25 2.25 0 1 1 1.5 0v.878a2.25 2.25 0 0 1-2.25 2.25h-1.5v2.128a2.251 2.251 0 1 1-1.5 0V8.5h-1.5A2.25 2.25 0 0 1 3.5 6.25v-.878a2.25 2.25 0 1 1 1.5 0ZM5 3.25a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Zm6.75.75a.75.75 0 1 0 0-1.5.75.75 0 0 0 0 1.5Zm-3 8.75a.75.75 0 1 0-1.5 0 .75.75 0 0 0 1.5 0Z"/>
53- </svg>
54- ${ forks }
55- </span>
56- ` ;
74+ // Find the existing star and fork spans
75+ const starSpan = element . querySelector ( '.repo-stars' ) ;
76+ const forkSpan = element . querySelector ( '.repo-forks' ) ;
77+
78+ if ( starSpan ) {
79+ // Get all child nodes
80+ const childNodes = Array . from ( starSpan . childNodes ) ;
81+
82+ // Find the text node (should be the last one)
83+ const textNode = childNodes . find ( node => node . nodeType === Node . TEXT_NODE ) ;
84+
85+ if ( textNode ) {
86+ // Update only the text content
87+ textNode . textContent = ` ${ stars } ` ;
88+ }
89+ }
90+
91+ if ( forkSpan ) {
92+ // Get all child nodes
93+ const childNodes = Array . from ( forkSpan . childNodes ) ;
94+
95+ // Find the text node (should be the last one)
96+ const textNode = childNodes . find ( node => node . nodeType === Node . TEXT_NODE ) ;
97+
98+ if ( textNode ) {
99+ // Update only the text content
100+ textNode . textContent = ` ${ forks } ` ;
101+ }
102+ }
57103 }
58104 }
59105} ) ;
0 commit comments