@@ -195,6 +195,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
195195 const topCompanies = problem . companies . slice ( 0 , 5 ) ;
196196 topCompanies . forEach ( ( company : { name : string ; } ) => {
197197 const button = document . createElement ( 'button' ) ;
198+ button . classList . add ( 'company-tag' ) ;
198199 button . onclick = ( ) => {
199200 chrome . runtime . sendMessage ( {
200201 action : 'openCompanyPage' ,
@@ -214,18 +215,7 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
214215
215216 chrome . storage . local . get ( [ 'isDarkTheme' ] , ( result ) => {
216217 const isDark = result . isDarkTheme ;
217- button . style . backgroundColor = isDark ? '#373737' : '#f3f4f5' ;
218- button . style . color = isDark ? '#fff' : '#1a1a1a' ;
219- button . style . border = `1px solid ${ isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)' } ` ;
220- 221- button . addEventListener ( 'mouseenter' , ( ) => {
222- button . style . backgroundColor = isDark ? '#424242' : '#e6e6e6' ;
223- button . style . borderColor = isDark ? 'rgba(255, 255, 255, 0.2)' : 'rgba(0, 0, 0, 0.2)' ;
224- } ) ;
225- button . addEventListener ( 'mouseleave' , ( ) => {
226- button . style . backgroundColor = isDark ? '#373737' : '#f3f4f5' ;
227- button . style . borderColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)' ;
228- } ) ;
218+ updateCompanyTagStyle ( button , isDark ) ;
229219 } ) ;
230220
231221 const icon = document . createElement ( 'img' ) ;
@@ -244,6 +234,69 @@ function loadCompanyTags(problemTitle: string, companyTagContainer: HTMLElement)
244234 return companyTagContainer ;
245235}
246236
237+ function updateCompanyTagStyle ( button : HTMLElement , isDark : boolean ) {
238+ button . style . backgroundColor = isDark ? '#373737' : '#f3f4f5' ;
239+ button . style . color = isDark ? '#fff' : '#1a1a1a' ;
240+ button . style . border = `1px solid ${ isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)' } ` ;
241+ 242+ // Remove existing listeners
243+ const oldMouseEnter = button . onmouseenter ;
244+ const oldMouseLeave = button . onmouseleave ;
245+ if ( oldMouseEnter ) button . removeEventListener ( 'mouseenter' , oldMouseEnter ) ;
246+ if ( oldMouseLeave ) button . removeEventListener ( 'mouseleave' , oldMouseLeave ) ;
247+ 248+ // Add new theme-aware listeners
249+ button . addEventListener ( 'mouseenter' , ( ) => {
250+ button . style . backgroundColor = isDark ? '#424242' : '#e6e6e6' ;
251+ button . style . borderColor = isDark ? 'rgba(255, 255, 255, 0.2)' : 'rgba(0, 0, 0, 0.2)' ;
252+ } ) ;
253+ button . addEventListener ( 'mouseleave' , ( ) => {
254+ button . style . backgroundColor = isDark ? '#373737' : '#f3f4f5' ;
255+ button . style . borderColor = isDark ? 'rgba(255, 255, 255, 0.1)' : 'rgba(0, 0, 0, 0.1)' ;
256+ } ) ;
257+ }
258+ 259+ function updateThemeForCompanyTags ( isDark : boolean ) {
260+ const companyTags = document . querySelectorAll ( '.company-tag' ) ;
261+ companyTags . forEach ( ( tag ) => {
262+ if ( tag instanceof HTMLElement ) {
263+ updateCompanyTagStyle ( tag , isDark ) ;
264+ }
265+ } ) ;
266+ }
267+ 268+ function setupDescriptionThemeListener ( ) {
269+ // Listen for LeetCode's theme changes
270+ const observer = new MutationObserver ( ( mutations ) => {
271+ mutations . forEach ( ( mutation ) => {
272+ if ( mutation . target instanceof HTMLElement && mutation . target . tagName === 'BODY' ) {
273+ chrome . storage . local . get ( [ 'themeMode' ] , ( result ) => {
274+ // Only sync theme if in auto mode
275+ if ( result . themeMode === 'auto' ) {
276+ const isDark = document . body . classList . contains ( 'dark' ) ;
277+ // Update our extension's theme setting
278+ chrome . storage . local . set ( { isDarkTheme : isDark } ) ;
279+ updateThemeForCompanyTags ( isDark ) ;
280+ }
281+ } ) ;
282+ }
283+ } ) ;
284+ } ) ;
285+ 286+ // Start observing the body element for class changes
287+ observer . observe ( document . body , {
288+ attributes : true ,
289+ attributeFilter : [ 'class' ]
290+ } ) ;
291+ 292+ // Also listen for our extension's theme changes
293+ chrome . storage . onChanged . addListener ( ( changes ) => {
294+ if ( changes . isDarkTheme ) {
295+ updateThemeForCompanyTags ( changes . isDarkTheme . newValue ) ;
296+ }
297+ } ) ;
298+ }
299+ 247300chrome . runtime . onMessage . addListener ( ( request , sender , sendResponse ) => {
248301 if ( request . action === 'updateDescription' ) {
249302 // Detect theme on first load of a problem page
@@ -252,6 +305,9 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
252305 showCompanyTags ( request . title . split ( '-' ) [ 0 ] . trim ( ) ) ;
253306 showDifficulty ( ) ;
254307 showRating ( request . title . split ( '-' ) [ 0 ] . trim ( ) ) ;
308+ 309+ // Add theme change listener after creating company tags
310+ setupDescriptionThemeListener ( ) ;
255311 } else if ( request . action === 'getTheme' ) {
256312 // Return the current LeetCode theme
257313 const htmlElement = document . documentElement ;
@@ -262,3 +318,4 @@ chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
262318 // Return true to indicate we will send a response asynchronously (needed for sendResponse)
263319 return true ;
264320} ) ;
321+
0 commit comments