1+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
2+ const base_url = window . location . pathname . split ( '/' ) [ 1 ] ;
3+ const json_url = '/' + base_url + "/versions.json" ;
4+ const stargazers_element = document . querySelector ( 'header div div:nth-child(3) a' ) ;
5+
6+ if ( ! stargazers_element ) {
7+ console . warn ( "Could not find the stargazers element to attach the version selector." ) ;
8+ return ;
9+ }
10+ fetch ( json_url )
11+ . then ( response => response . json ( ) )
12+ . then ( data => {
13+ const container = document . createElement ( "div" ) ;
14+ container . style . display = "flex" ;
15+ container . style . alignItems = "center" ;
16+ container . style . marginLeft = "12px" ; // Space between stars and version
17+
18+ // 2. Create the select element
19+ const select = document . createElement ( "select" ) ;
20+ select . id = "version-selector" ;
21+ select . style . padding = "4px 8px" ;
22+ select . style . border = "1px solid #444" ;
23+ select . style . borderRadius = "6px" ;
24+ select . style . fontSize = "12px" ;
25+ select . style . fontWeight = "600" ;
26+ select . onchange = function ( ) { window . location . href = this . value ; } ;
27+
28+ // 3. Populate options
29+ const currentPath = window . location . pathname ;
30+ data . forEach ( v => {
31+ const opt = document . createElement ( "option" ) ;
32+ const isLatest = v . aliases . includes ( "latest" ) ;
33+
34+ // If it's the latest, point to the /latest/ alias instead of the version folder
35+ opt . value = isLatest ? `/${ base_url } /latest/` : `/${ base_url } /${ v . version } /` ;
36+
37+ opt . textContent = isLatest ? `${ v . title } (latest)` : v . title ;
38+
39+ if ( currentPath . includes ( `/${ v . version } /` ) || ( isLatest && currentPath . includes ( '/latest/' ) ) ) {
40+ opt . selected = true ;
41+ }
42+ select . appendChild ( opt ) ;
43+ } ) ;
44+
45+ container . appendChild ( select ) ;
46+
47+ stargazers_element . insertAdjacentElement ( 'afterend' , container ) ;
48+ } )
49+ . catch ( error => console . error ( "Error loading versions:" , error ) ) ;
50+ } ) ;
0 commit comments