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 60a9a01

Browse files
committed
fix dynamic routing not resetting solutions tab elements
1 parent af563b2 commit 60a9a01

File tree

1 file changed

+27
-16
lines changed

1 file changed

+27
-16
lines changed

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

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,12 @@ function showContent(type: 'Discussion' | 'Video' | 'Code') {
210210
(section as HTMLElement).style.display = 'none';
211211
});
212212

213+
// Get the language buttons container
214+
const languageButtons = document.querySelector('.language-buttons-container') as HTMLElement;
215+
if (languageButtons) {
216+
languageButtons.style.display = 'none'; // Hide by default
217+
}
218+
213219
// Show the selected content
214220
switch (type) {
215221
case 'Video':
@@ -221,9 +227,13 @@ function showContent(type: 'Discussion' | 'Video' | 'Code') {
221227
break;
222228
case 'Code':
223229
const codeSection = document.querySelector('.code-section') as HTMLElement;
224-
const languageButtons = document.querySelector('.language-buttons-container') as HTMLElement;
225-
if (codeSection) codeSection.style.display = 'block';
226-
if (languageButtons) languageButtons.style.display = 'flex';
230+
if (codeSection) {
231+
codeSection.style.display = 'block';
232+
// Only show language buttons when code section is active
233+
if (languageButtons) {
234+
languageButtons.style.display = 'flex';
235+
}
236+
}
227237
break;
228238
case 'Discussion':
229239
// No need to do anything as the discussion is the default content
@@ -584,11 +594,21 @@ function updateSolutionsTab(title: string) {
584594
let retryCount = 0;
585595

586596
const tryInsertContent = () => {
587-
// First check if we already have a wrapper to prevent duplicates
597+
// Check if we already have a wrapper and if it's for the same problem
588598
const existingWrapper = document.querySelector('.leetcode-explained-wrapper');
589599
if (existingWrapper) {
590-
console.log('Content already exists, skipping insertion');
591-
return;
600+
const currentTitle = document.title.split('-')[0].trim();
601+
const wrapperTitle = existingWrapper.getAttribute('data-problem-title');
602+
603+
// If it's the same problem, preserve the state
604+
if (wrapperTitle === currentTitle) {
605+
console.log('Content for same problem exists, preserving state');
606+
return;
607+
}
608+
609+
// If it's a different problem, remove the old wrapper
610+
console.log('Different problem detected, updating content');
611+
existingWrapper.remove();
592612
}
593613

594614
const searchBar = document.querySelectorAll('input.block')[0]?.parentElement?.parentElement?.parentElement;
@@ -607,12 +627,6 @@ function updateSolutionsTab(title: string) {
607627

608628
// If still not found, set up a MutationObserver to watch for DOM changes
609629
const observer = new MutationObserver((mutations, obs) => {
610-
// Check again for existing wrapper before proceeding
611-
if (document.querySelector('.leetcode-explained-wrapper')) {
612-
obs.disconnect();
613-
return;
614-
}
615-
616630
const searchBar = document.querySelectorAll('input.block')[0]?.parentElement?.parentElement?.parentElement;
617631
if (searchBar) {
618632
obs.disconnect(); // Stop observing once we find the element
@@ -645,12 +659,9 @@ function insertContent(searchBar: Element, title: string, result: any) {
645659
if (!problem?.videos && !problem?.languages) return;
646660
if (problem.videos?.length === 0 && problem.languages?.length === 0) return;
647661

648-
// Remove any existing containers
649-
const existingWrapper = document.querySelector('.leetcode-explained-wrapper');
650-
if (existingWrapper) existingWrapper.remove();
651-
652662
// Create wrapper for all our custom content
653663
const wrapper = createCustomContentWrapper();
664+
wrapper.setAttribute('data-problem-title', problemTitle);
654665

655666
// Create and add nav container
656667
const navContainer = createNavContainer(problem);

0 commit comments

Comments
(0)

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