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 21bd1b2

Browse files
committed
fix dynamic routing, but refreshing removes solution tab update
1 parent a85d4eb commit 21bd1b2

File tree

3 files changed

+44
-37
lines changed

3 files changed

+44
-37
lines changed

‎src/background/background.ts‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,33 +71,44 @@ chrome.runtime.onMessage.addListener((request) => {
7171
});
7272

7373
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
74-
if (changeInfo.status==='complete'&&tab.url) {
74+
if (tab.url) {
7575
const url = tab.url;
7676
let problemUrl = /^https:\/\/leetcode\.com\/problems\/.*\/?/;
77+
78+
// Only proceed if this is a leetcode problem page
7779
if (url.match(problemUrl)) {
78-
chrome.storage.local.get(['currentLeetCodeProblemTitle', 'descriptionTabUpdated', 'solutionsTabUpdated'], (result) => {
80+
// Extract the problem path from the URL
81+
const problemPath = url.match(/\/problems\/([^/]+)/)?.[1];
82+
83+
chrome.storage.local.get(['currentLeetCodeProblem', 'currentLeetCodeProblemTitle', 'descriptionTabUpdated', 'solutionsTabUpdated'], (result) => {
84+
let lastProblem = result.currentLeetCodeProblem || '';
7985
let lastTitle = result.currentLeetCodeProblemTitle || '';
8086
let descriptionTabUpdated = result.descriptionTabUpdated || false;
8187
let solutionsTabUpdated = result.solutionsTabUpdated || false;
82-
if (tab.title !== lastTitle) {
88+
89+
// Only reset if we've switched to a different problem
90+
if (problemPath && problemPath !== lastProblem) {
91+
console.log('Problem changed from', lastProblem, 'to', problemPath);
8392
chrome.storage.local.set({
93+
'currentLeetCodeProblem': problemPath,
8494
'currentLeetCodeProblemTitle': tab.title,
8595
'descriptionTabUpdated': false,
8696
'solutionsTabUpdated': false
8797
});
88-
// If the title has changed, we reset both flags
8998
descriptionTabUpdated = false;
9099
solutionsTabUpdated = false;
91100
}
92101

93-
let descriptionUrl = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?/;
102+
// If the description tab has not been updated and the url matches the description page, we update the flag
103+
let descriptionUrl = /^https:\/\/leetcode\.com\/problems\/.*\/(description\/)?$/;
94104
if (!descriptionTabUpdated && url.match(descriptionUrl)) {
95105
chrome.storage.local.set({ 'descriptionTabUpdated': true });
96106
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: tab.title || 'title' });
97107
}
98108

99-
let solutionsUrl = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?/;
100-
if (url.match(solutionsUrl)) {
109+
// If the solutions tab has not been updated and the url matches the solutions page, we update the flag
110+
let solutionsUrl = /^https:\/\/leetcode\.com\/problems\/.*\/solutions\/?$/;
111+
if (!solutionsTabUpdated && url.match(solutionsUrl)) {
101112
chrome.storage.local.set({ 'solutionsTabUpdated': true });
102113
chrome.tabs.sendMessage(tabId, { action: 'updateSolutions', title: tab.title || 'title' });
103114
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ function setupDescriptionThemeListener() {
371371
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
372372
if (request.action === 'updateDescription') {
373373
// Detect theme on first load of a problem page
374+
console.log('Updating description tab...');
374375
detectAndSyncTheme();
375376
showExamples();
376377
showCompanyTags(request.title.split('-')[0].trim());
@@ -400,8 +401,8 @@ function initializeContentScript() {
400401
}
401402

402403
function onDOMReady() {
403-
// Check if we're on a LeetCode problem page
404-
const isProblemPage = /^https:\/\/leetcode\.com\/problems\/.*\/?/.test(window.location.href);
404+
// Check if we're on a LeetCode problem's description page
405+
const isProblemPage = /^https:\/\/leetcode\.com\/problems\/.*\/description\/?/.test(window.location.href);
405406

406407
if (isProblemPage) {
407408
console.log('LeetCode problem page detected, initializing content script...');
@@ -420,7 +421,7 @@ function initializeContentScript() {
420421
// Add theme change listener after creating company tags
421422
setupDescriptionThemeListener();
422423

423-
console.log('Content script initialized for problem:', problemTitle);
424+
console.log('Description tab content script initialized for problem:', problemTitle);
424425
}
425426
}
426427
}

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

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -538,49 +538,44 @@ chrome.runtime.onMessage.addListener((request) => {
538538
const problem = result.leetcodeProblems.questions.find((problem: { title: string }) => problem.title === title);
539539

540540
// If no solution code or videos exist, dont do anything.
541-
if (!problem.videos && !problem.languages) return;
542-
if (problem.videos.length == 0 && problem.languages.length == 0) {
541+
if (!problem?.videos && !problem?.languages) return;
542+
if (problem.videos?.length == 0 && problem.languages?.length == 0) {
543543
return;
544544
}
545545

546-
// If forceUpdate is true or this is not a video update, remove existing containers
547-
if (request.forceUpdate) {
548-
const existingContainers = [
549-
'.nav-container',
550-
'.video-container',
551-
'.code-container',
552-
'.language-buttons-container'
553-
].forEach(selector => {
554-
const element = document.querySelector(selector);
555-
if (element) element.remove();
556-
});
557-
}
546+
// Always remove existing containers when updating solutions
547+
const existingContainers = [
548+
'.nav-container',
549+
'.video-container',
550+
'.code-container',
551+
'.language-buttons-container'
552+
].forEach(selector => {
553+
const element = document.querySelector(selector);
554+
if (element) element.remove();
555+
});
558556

559-
// Only create nav container if it doesn't exist or if this is not a video update
560-
let existingNavContainer = document.querySelector('.nav-container');
561-
if (!existingNavContainer) {
562-
const newNavContainer = createNavContainer(problem);
563-
searchBar?.insertBefore(newNavContainer, searchBar.firstChild);
564-
}
557+
// Create new nav container
558+
const newNavContainer = createNavContainer(problem);
559+
searchBar?.insertBefore(newNavContainer, searchBar.firstChild);
565560

566-
// Check if the video container already exists before adding
567-
if (!document.querySelector('.video-container')&&problem.videos.length > 0) {
561+
// Add video container if videos exist
562+
if (problem.videos?.length > 0) {
568563
let videoContainer = createVideoContainer(problem);
569564
if (searchBar) searchBar.insertBefore(videoContainer, searchBar.children[1]);
570565
}
571566

572-
// Check if the code container already exists before adding
573-
if (!document.querySelector('.code-container')&&problem.languages.length > 0) {
567+
// Add code container if languages exist
568+
if (problem.languages?.length > 0) {
574569
let codeContainer = createCodeContainer();
575570
if (searchBar) searchBar.insertBefore(codeContainer, searchBar.children[1]);
576571
}
577572

578-
// Check if the language buttons container already exists before adding
579-
if (!document.querySelector('.language-buttons-container')) {
573+
// Add language buttons container if languages exist
574+
if (problem.languages?.length>0) {
580575
let languageButtonsContainer = createLanguageButtons(problem);
581576
languageButtonsContainer.classList.add('language-buttons-container');
582577
languageButtonsContainer.style.display = 'none';
583-
if (searchBar) searchBar.insertBefore(languageButtonsContainer, searchBar.children[1]);// Or choose a different position
578+
if (searchBar) searchBar.insertBefore(languageButtonsContainer, searchBar.children[1]);
584579
}
585580

586581
// Add theme change listener after creating containers

0 commit comments

Comments
(0)

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