@@ -144,29 +144,7 @@ function showRating(problemTitle: string) {
144144// show the company tags if the user has enabled it in the settings
145145function showCompanyTags ( problemTitle : string ) {
146146 chrome . storage . local . get ( [ 'showCompanyTags' ] , ( result ) => {
147- const showCompanyTags = result . showCompanyTags ;
148- let companyTagContainer = document . getElementById ( 'companyTagContainer' ) ;
149- 150- // First handle visibility
151- if ( ! showCompanyTags ) {
152- if ( companyTagContainer ) {
153- companyTagContainer . style . display = 'none' ;
154- }
155- return ;
156- } else if ( companyTagContainer && companyTagContainer instanceof HTMLElement ) {
157- companyTagContainer . style . display = 'flex' ;
158- // If container exists and is visible, just update styles
159- chrome . storage . local . get ( [ 'isDarkTheme' ] , ( result ) => {
160- const isDark = result . isDarkTheme ;
161- const tags = companyTagContainer ?. querySelectorAll ( '.company-tag' ) ;
162- if ( tags ) {
163- tags . forEach ( tag => {
164- if ( tag instanceof HTMLElement ) {
165- updateCompanyTagStyle ( tag , isDark ) ;
166- }
167- } ) ;
168- }
169- } ) ;
147+ if ( ! result . showCompanyTags ) {
170148 return ;
171149 }
172150
@@ -175,6 +153,26 @@ function showCompanyTags(problemTitle: string) {
175153 const baseDelay = 300 ;
176154 let retryCount = 0 ;
177155
156+ const insertCompanyTags = ( description : Element ) => {
157+ // Double check for existing container before inserting
158+ if ( document . getElementById ( 'companyTagContainer' ) ) {
159+ return ;
160+ }
161+ 162+ // Create new container
163+ const newCompanyTagContainer = document . createElement ( 'div' ) ;
164+ newCompanyTagContainer . id = 'companyTagContainer' ;
165+ newCompanyTagContainer . style . display = 'flex' ;
166+ newCompanyTagContainer . style . flexDirection = 'row' ;
167+ newCompanyTagContainer . style . marginBottom = '20px' ;
168+ newCompanyTagContainer . style . gap = '5px' ;
169+ 170+ description . insertBefore ( newCompanyTagContainer , description . firstChild ) ;
171+ 172+ // Load and inject company tags
173+ loadCompanyTags ( problemTitle , newCompanyTagContainer ) ;
174+ } ;
175+ 178176 const tryInsertCompanyTags = ( ) => {
179177 // First check if container already exists to prevent duplicates
180178 if ( document . getElementById ( 'companyTagContainer' ) ) {
@@ -219,30 +217,11 @@ function showCompanyTags(problemTitle: string) {
219217 return ;
220218 }
221219
220+ // If we found the description element, insert the company tags
222221 insertCompanyTags ( description ) ;
223222 } ;
224223
225- const insertCompanyTags = ( description : Element ) => {
226- // Double check for existing container before inserting
227- if ( document . getElementById ( 'companyTagContainer' ) ) {
228- return ;
229- }
230- 231- // Create new container
232- const newCompanyTagContainer = document . createElement ( 'div' ) ;
233- newCompanyTagContainer . id = 'companyTagContainer' ;
234- newCompanyTagContainer . style . display = 'flex' ;
235- newCompanyTagContainer . style . flexDirection = 'row' ;
236- newCompanyTagContainer . style . marginBottom = '20px' ;
237- newCompanyTagContainer . style . gap = '5px' ;
238- 239- description . insertBefore ( newCompanyTagContainer , description . firstChild ) ;
240- 241- // Load and inject company tags
242- loadCompanyTags ( problemTitle , newCompanyTagContainer ) ;
243- } ;
244- 245- // Start the retry process
224+ // Start the process
246225 tryInsertCompanyTags ( ) ;
247226 } ) ;
248227}
@@ -401,31 +380,41 @@ function initializeContentScript() {
401380 }
402381
403382 function onDOMReady ( ) {
404- // Check if we're on a LeetCode problem's description page
405- const isProblemPage = / ^ h t t p s : \/ \/ l e e t c o d e \. c o m \/ p r o b l e m s \/ . * \/ d e s c r i p t i o n \/ ? / . test ( window . location . href ) ;
383+ // Set up theme detection and synchronization
384+ setupDescriptionThemeListener ( ) ;
406385
407- if ( isProblemPage ) {
408- console . log ( 'LeetCode problem page detected, initializing content script...' ) ;
409- 410- // Extract the problem title from the page title
411- const pageTitle = document . title ;
412- const problemTitle = pageTitle . split ( '-' ) [ 0 ] . trim ( ) ;
413- 414- // Detect theme on first load of a problem page
415- detectAndSyncTheme ( ) ;
416- showExamples ( ) ;
417- showCompanyTags ( problemTitle ) ;
418- showDifficulty ( ) ;
419- showRating ( problemTitle ) ;
420- 421- // Add theme change listener after creating company tags
422- setupDescriptionThemeListener ( ) ;
423- 424- console . log ( 'Description tab content script initialized for problem:' , problemTitle ) ;
386+ // Get the problem title from the page
387+ const problemTitle = document . title . replace ( ' - LeetCode' , '' ) ;
388+ 389+ // Apply all enhancements
390+ showDifficulty ( ) ;
391+ showRating ( problemTitle ) ;
392+ showCompanyTags ( problemTitle ) ;
393+ showExamples ( ) ;
394+ 395+ // Set up a MutationObserver to detect tab changes
396+ const observer = new MutationObserver ( ( mutations ) => {
397+ mutations . forEach ( ( mutation ) => {
398+ if ( mutation . type === 'childList' && mutation . addedNodes . length > 0 ) {
399+ // Check if we're on the description tab
400+ const descriptionTab = document . querySelector ( '[data-cy="description-tab"]' ) ;
401+ if ( descriptionTab && descriptionTab . classList . contains ( 'active' ) ) {
402+ // Re-apply company tags when switching to description tab
403+ const problemTitle = document . title . replace ( ' - LeetCode' , '' ) ;
404+ showCompanyTags ( problemTitle ) ;
405+ }
406+ }
407+ } ) ;
408+ } ) ;
409+ 410+ // Start observing the tab container
411+ const tabContainer = document . querySelector ( '[role="tablist"]' ) ;
412+ if ( tabContainer ) {
413+ observer . observe ( tabContainer , { childList : true , subtree : true } ) ;
425414 }
426415 }
427416}
428417
429- // Run the initialization
418+ // Initialize the content script
430419initializeContentScript ( ) ;
431420
0 commit comments