Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 921ce7e

Browse files
committed
fix opening page to solutions tab not loading company tags
1 parent 280f211 commit 921ce7e

File tree

1 file changed

+76
-21
lines changed

1 file changed

+76
-21
lines changed

‎src/content-script/update-description-tab.ts

Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -412,38 +412,93 @@ function initializeDescriptionTab() {
412412
// Set up theme detection and synchronization
413413
setupDescriptionThemeListener();
414414

415-
// Get the problem title from the page
416-
constproblemTitle=document.title.replace(' - LeetCode','');
415+
// Initial load of enhancements
416+
updatePageContent();
417417

418-
// Apply all enhancements
419-
showDifficulty();
420-
showRating(problemTitle);
421-
showCompanyTags(problemTitle);
422-
showExamples();
423-
424-
// Set up a MutationObserver to detect tab changes
418+
// Set up URL change detection using History API
419+
const originalPushState = history.pushState;
420+
const originalReplaceState = history.replaceState;
421+
422+
history.pushState = function(data: any, unused: string, url?: string | URL) {
423+
originalPushState.call(this, data, unused, url);
424+
handleUrlChange();
425+
};
426+
427+
history.replaceState = function(data: any, unused: string, url?: string | URL) {
428+
originalReplaceState.call(this, data, unused, url);
429+
handleUrlChange();
430+
};
431+
432+
window.addEventListener('popstate', handleUrlChange);
433+
434+
// Set up a MutationObserver to detect tab and content changes
425435
const observer = new MutationObserver((mutations) => {
436+
let shouldUpdate = false;
437+
426438
mutations.forEach((mutation) => {
427-
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
428-
// Check if we're on the description tab
429-
const descriptionTab = document.querySelector('[data-cy="description-tab"]');
430-
if (descriptionTab && descriptionTab.classList.contains('active')) {
431-
// Re-apply company tags when switching to description tab
432-
const problemTitle = document.title.replace(' - LeetCode', '');
433-
showCompanyTags(problemTitle);
439+
// Check for tab changes
440+
if (mutation.target instanceof HTMLElement) {
441+
const isTabChange = mutation.target.getAttribute('role') === 'tab' ||
442+
mutation.target.closest('[role="tab"]');
443+
if (isTabChange) {
444+
shouldUpdate = true;
434445
}
435446
}
447+
448+
// Check for content changes in the main container
449+
if (mutation.type === 'childList' &&
450+
((mutation.target instanceof HTMLElement && mutation.target.classList?.contains('elfjS')) ||
451+
mutation.addedNodes.length > 0)) {
452+
shouldUpdate = true;
453+
}
436454
});
455+
456+
if (shouldUpdate) {
457+
// Small delay to ensure DOM is fully updated
458+
setTimeout(updatePageContent, 100);
459+
}
437460
});
438461

439-
// Start observing the tab container
440-
const tabContainer = document.querySelector('[role="tablist"]');
441-
if (tabContainer) {
442-
observer.observe(tabContainer, { childList: true, subtree: true });
443-
}
462+
// Observe both the tab container and the main content area
463+
observer.observe(document.body, {
464+
childList: true,
465+
subtree: true,
466+
attributes: true,
467+
attributeFilter: ['class', 'data-cy']
468+
});
469+
}
470+
}
471+
472+
// Update all page content
473+
function updatePageContent() {
474+
const problemTitle = document.title.replace(' - LeetCode', '').split('-')[0].trim();
475+
const isDescriptionTab = isOnDescriptionTab();
476+
477+
if (isDescriptionTab) {
478+
showCompanyTags(problemTitle);
479+
showDifficulty();
480+
showRating(problemTitle);
481+
showExamples();
444482
}
445483
}
446484

485+
// Check if we're on the description tab
486+
function isOnDescriptionTab() {
487+
// Check multiple conditions to determine if we're on the description tab
488+
const descriptionTab = document.querySelector('[data-cy="description-tab"]');
489+
const isDescriptionActive = descriptionTab?.classList.contains('active');
490+
const notOnSolutions = !window.location.href.includes('/solutions');
491+
const hasDescriptionContent = !!document.getElementsByClassName('elfjS')[0];
492+
493+
return (isDescriptionActive || notOnSolutions) && hasDescriptionContent;
494+
}
495+
496+
// Handle URL changes
497+
function handleUrlChange() {
498+
// Small delay to ensure DOM is updated
499+
setTimeout(updatePageContent, 200);
500+
}
501+
447502
// Initialize the content script
448503
initializeDescriptionTab();
449504

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /