13
13
var currentTheme = document . getElementById ( "themeStyle" ) ;
14
14
var mainTheme = document . getElementById ( "mainThemeStyle" ) ;
15
15
16
+ var savedHref = [ ] ;
17
+
18
+ function onEach ( arr , func ) {
19
+ if ( arr && arr . length > 0 && func ) {
20
+ for ( var i = 0 ; i < arr . length ; i ++ ) {
21
+ if ( func ( arr [ i ] ) === true ) {
22
+ break ;
23
+ }
24
+ }
25
+ }
26
+ }
27
+
16
28
function updateLocalStorage ( name , value ) {
17
29
if ( typeof ( Storage ) !== "undefined" ) {
18
30
localStorage [ name ] = value ;
@@ -29,8 +41,24 @@ function getCurrentValue(name) {
29
41
}
30
42
31
43
function switchTheme ( styleElem , mainStyleElem , newTheme ) {
32
- styleElem . href = mainStyleElem . href . replace ( "rustdoc.css" , newTheme + ".css" ) ;
33
- updateLocalStorage ( 'rustdoc-theme' , newTheme ) ;
44
+ var newHref = mainStyleElem . href . replace ( "rustdoc.css" , newTheme + ".css" ) ;
45
+ var found = false ;
46
+
47
+ if ( savedHref . length === 0 ) {
48
+ onEach ( document . getElementsByTagName ( "link" ) , function ( el ) {
49
+ savedHref . push ( el . href ) ;
50
+ } ) ;
51
+ }
52
+ onEach ( savedHref , function ( el ) {
53
+ if ( el === newHref ) {
54
+ found = true ;
55
+ return true ;
56
+ }
57
+ } ) ;
58
+ if ( found === true ) {
59
+ styleElem . href = newHref ;
60
+ updateLocalStorage ( 'rustdoc-theme' , newTheme ) ;
61
+ }
34
62
}
35
63
36
- switchTheme ( currentTheme , mainTheme , getCurrentValue ( 'theme' ) || 'main' ) ;
64
+ switchTheme ( currentTheme , mainTheme , getCurrentValue ( 'rustdoc- theme' ) || 'main' ) ;
0 commit comments