@@ -13,19 +13,27 @@ let toggleThemeSetting = () => {
1313} ;
1414
1515// Change the theme setting and apply the theme.
16- let setThemeSetting = ( themeSetting ) => {
17- localStorage . setItem ( "theme" , themeSetting ) ;
16+ let setThemeSetting = ( themeSetting , init = false ) => {
17+ try {
18+ if ( localStorage . getItem ( "theme" ) !== themeSetting ) {
19+ localStorage . setItem ( "theme" , themeSetting ) ;
20+ }
21+ } catch ( e ) {
22+ console . debug ( "localStorage is not available:" , e ) ;
23+ }
1824
1925 document . documentElement . setAttribute ( "data-theme-setting" , themeSetting ) ;
2026
21- applyTheme ( ) ;
27+ applyTheme ( init ) ;
2228} ;
2329
2430// Apply the computed dark or light theme to the website.
25- let applyTheme = ( ) => {
31+ let applyTheme = ( init = false ) => {
2632 let theme = determineComputedTheme ( ) ;
2733
28- transTheme ( ) ;
34+ if ( ! init ) {
35+ transTheme ( ) ;
36+ }
2937 setHighlight ( theme ) ;
3038 setGiscusTheme ( theme ) ;
3139 setSearchTheme ( theme ) ;
@@ -72,18 +80,24 @@ let applyTheme = () => {
7280 // Set jupyter notebooks themes.
7381 let jupyterNotebooks = document . getElementsByClassName ( "jupyter-notebook-iframe-container" ) ;
7482 for ( let i = 0 ; i < jupyterNotebooks . length ; i ++ ) {
75- let bodyElement = jupyterNotebooks [ i ] . getElementsByTagName ( "iframe" ) [ 0 ] . contentWindow . document . body ;
76- if ( theme == "dark" ) {
77- bodyElement . setAttribute ( "data-jp-theme-light" , "false" ) ;
78- bodyElement . setAttribute ( "data-jp-theme-name" , "JupyterLab Dark" ) ;
79- } else {
80- bodyElement . setAttribute ( "data-jp-theme-light" , "true" ) ;
81- bodyElement . setAttribute ( "data-jp-theme-name" , "JupyterLab Light" ) ;
83+ try {
84+ let bodyElement = jupyterNotebooks [ i ] . getElementsByTagName ( "iframe" ) [ 0 ] . contentWindow . document . body ;
85+ if ( bodyElement ) {
86+ if ( theme == "dark" ) {
87+ bodyElement . setAttribute ( "data-jp-theme-light" , "false" ) ;
88+ bodyElement . setAttribute ( "data-jp-theme-name" , "JupyterLab Dark" ) ;
89+ } else {
90+ bodyElement . setAttribute ( "data-jp-theme-light" , "true" ) ;
91+ bodyElement . setAttribute ( "data-jp-theme-name" , "JupyterLab Light" ) ;
92+ }
93+ }
94+ } catch ( e ) {
95+ console . warn ( "Could not access jupyter notebook iframe document:" , e ) ;
8296 }
8397 }
8498
8599 // Updates the background of medium-zoom overlay.
86- if ( typeof medium_zoom !== "undefined" ) {
100+ if ( ! init && typeof medium_zoom !== "undefined" ) {
87101 medium_zoom . update ( {
88102 background : getComputedStyle ( document . documentElement ) . getPropertyValue ( "--global-bg-color" ) + "ee" , // + 'ee' for trasparency.
89103 } ) ;
@@ -292,7 +306,7 @@ let determineComputedTheme = () => {
292306let initTheme = ( ) => {
293307 let themeSetting = determineThemeSetting ( ) ;
294308
295- setThemeSetting ( themeSetting ) ;
309+ setThemeSetting ( themeSetting , true ) ;
296310
297311 // Theme toggle button is removed - no event listener needed
298312 // Users cannot manually change the theme
0 commit comments