@@ -20,6 +20,61 @@ interface Problem {
2020 // Add other properties as needed 
2121} 
2222
23+ // Detect LeetCode's theme and set extension theme accordingly 
24+ function  detectAndSyncTheme ( )  { 
25+  chrome . storage . local . get ( [ 'themeMode' ] ,  ( result )  =>  { 
26+  // Only sync theme if in auto mode 
27+  if  ( result . themeMode  !==  'auto' )  { 
28+  return ; 
29+  } 
30+ 31+  // Get the current LeetCode theme from HTML tag 
32+  const  htmlElement  =  document . documentElement ; 
33+  const  leetcodeTheme  =  htmlElement . classList . contains ( 'dark' )  ? 'dark'  : 'light' ; 
34+ 35+  // Set the extension theme based on LeetCode's theme 
36+  chrome . storage . local . set ( {  
37+  isDarkTheme : leetcodeTheme  ===  'dark' 
38+  } ) ; 
39+ 40+  console . log ( `Theme auto-detected: ${ leetcodeTheme }  ) ; 
41+ 42+  // Set up observer for future theme changes 
43+  observeThemeChanges ( ) ; 
44+  } ) ; 
45+ } 
46+ 47+ // Observe theme changes in LeetCode and update extension theme 
48+ function  observeThemeChanges ( )  { 
49+  chrome . storage . local . get ( [ 'themeMode' ] ,  ( result )  =>  { 
50+  // Only observe changes if theme mode is set to 'auto' 
51+  if  ( result . themeMode  !==  'auto' )  { 
52+  return ; 
53+  } 
54+ 55+  const  htmlElement  =  document . documentElement ; 
56+ 57+  // Create a new observer 
58+  const  observer  =  new  MutationObserver ( ( mutations )  =>  { 
59+  mutations . forEach ( ( mutation )  =>  { 
60+  if  ( mutation . attributeName  ===  'class' )  { 
61+  const  leetcodeTheme  =  htmlElement . classList . contains ( 'dark' )  ? 'dark'  : 'light' ; 
62+  chrome . storage . local . set ( {  
63+  isDarkTheme : leetcodeTheme  ===  'dark' 
64+  } ) ; 
65+  console . log ( `Theme changed to: ${ leetcodeTheme }  ) ; 
66+  } 
67+  } ) ; 
68+  } ) ; 
69+ 70+  // Start observing 
71+  observer . observe ( htmlElement ,  { 
72+  attributes : true , 
73+  attributeFilter : [ 'class' ] 
74+  } ) ; 
75+  } ) ; 
76+ } 
77+ 2378// show the leetcode difficulty if the user has enabled it in the settings 
2479function  showDifficulty ( )  { 
2580 chrome . storage . local . get ( [ 'showDifficulty' ] ,  ( result )  =>  { 
@@ -191,11 +246,21 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
191246 return  companyTagContainer ; 
192247} 
193248
194- chrome . runtime . onMessage . addListener ( ( request )  =>  { 
249+ chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse )  =>  { 
195250 if  ( request . action  ===  'updateDescription' )  { 
251+  // Detect theme on first load of a problem page 
252+  detectAndSyncTheme ( ) ; 
196253 showExamples ( ) ; 
197254 showCompanyTags ( request . title . split ( '-' ) [ 0 ] . trim ( ) ) ; 
198255 showDifficulty ( ) ; 
199256 showRating ( request . title . split ( '-' ) [ 0 ] . trim ( ) ) ; 
257+  }  else  if  ( request . action  ===  'getTheme' )  { 
258+  // Return the current LeetCode theme 
259+  const  htmlElement  =  document . documentElement ; 
260+  const  leetcodeTheme  =  htmlElement . classList . contains ( 'dark' )  ? 'dark'  : 'light' ; 
261+  sendResponse ( {  theme : leetcodeTheme  } ) ; 
200262 } 
263+ 264+  // Return true to indicate we will send a response asynchronously (needed for sendResponse) 
265+  return  true ; 
201266} ) ; 
0 commit comments