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 d7d664c

Browse files
authored
Merge pull request #67 from zubyj/fix-description-tab-updating-on-click
Improve View Detection Logic for Description ↔ Editor Switches
2 parents 96e419a + c3be99e commit d7d664c

File tree

2 files changed

+84
-25
lines changed

2 files changed

+84
-25
lines changed

‎src/background/background.ts

Lines changed: 54 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,11 @@ chrome.runtime.onMessage.addListener((request) => {
7373
// Keep track of the last state to avoid duplicate updates
7474
let lastState = {
7575
problemPath: '',
76-
view: '', // 'problem' or 'solutions'
76+
view: '', // 'problem' or 'solutions' or 'description'
7777
lastPathname: '', // Track full pathname to detect real navigation
7878
lastUrl: '', // Track full URL to detect refreshes
79-
lastUpdateTime: 0 // Track time of last update to prevent rapid re-triggers
79+
lastUpdateTime: 0, // Track time of last update to prevent rapid re-triggers
80+
lastTabId: 0 // Track the last tab ID to help distinguish between refreshes and switches
8081
};
8182

8283
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
@@ -86,12 +87,19 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
8687

8788
// Check if this is a leetcode problem page
8889
if (url.match(problemUrl)) {
89-
// Extract the problem path from the URL
90-
const problemPath = url.match(/\/problems\/([^/]+)/)?.[1];
90+
// Extract the problem path from the URL and ensure it exists
91+
const problemPathMatch = url.match(/\/problems\/([^/]+)/);
92+
if (!problemPathMatch?.[1]) return;
93+
const problemPath = problemPathMatch[1];
9194
const pathname = new URL(url).pathname;
9295

93-
// Determine the current view - now only distinguishing between problem view and solutions
94-
let currentView = url.includes('/solutions') ? 'solutions' : 'problem';
96+
// More precise view detection
97+
let currentView = 'problem'; // default to problem view
98+
if (url.includes('/solutions')) {
99+
currentView = 'solutions';
100+
} else if (url.includes('/description')) {
101+
currentView = 'description';
102+
}
95103

96104
// Only trigger updates on actual page loads or problem changes
97105
const isPageLoad = changeInfo.status === 'complete';
@@ -103,42 +111,56 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
103111
currentView === 'solutions' &&
104112
lastState.view === 'solutions' &&
105113
problemPath === lastState.problemPath;
114+
115+
// Check if this is a navigation between description and editor
116+
const isDescriptionEditorSwitch =
117+
!isInternalSolutionsNavigation &&
118+
problemPath === lastState.problemPath &&
119+
((currentView === 'problem' && lastState.view === 'description') ||
120+
(currentView === 'description' && lastState.view === 'problem'));
106121

107-
// Detect actual page refresh vs internal navigation
122+
// Detect actual page refresh with improved conditions
108123
const isActualRefresh =
109124
url === lastState.lastUrl &&
110125
isPageLoad &&
111126
changeInfo.url === undefined &&
112127
!isInternalSolutionsNavigation &&
128+
!isDescriptionEditorSwitch &&
129+
currentView === lastState.view &&
130+
tabId === lastState.lastTabId && // Same tab for refresh
113131
Date.now() - lastState.lastUpdateTime > 1000;
114132

115133
const isRealNavigation =
116134
!isInternalSolutionsNavigation &&
135+
!isDescriptionEditorSwitch &&
117136
((pathname !== lastState.lastPathname || isViewChange) &&
118137
!pathname.includes('playground') &&
119138
!pathname.includes('editor') &&
120139
!pathname.includes('interpret-solution') &&
121140
!pathname.includes('submissions'));
122141

123-
// Update last URL and time
124-
if (!isInternalSolutionsNavigation) {
125-
lastState.lastUrl = url;
126-
}
127-
128-
// Only update if there's a real navigation, problem change, or actual refresh
129-
if ((isProblemChange || (isViewChange && isRealNavigation) || isActualRefresh) && problemPath) {
130-
console.log(`State change detected - ${
131-
isProblemChange ? 'New Problem' :
132-
isViewChange ? 'View Changed' :
133-
isActualRefresh ? 'Page Refresh' :
134-
'Page Load'
135-
}`);
142+
// Update state tracking
143+
const shouldUpdateState =
144+
isProblemChange ||
145+
isViewChange ||
146+
isActualRefresh ||
147+
tabId !== lastState.lastTabId;
148+
149+
if (shouldUpdateState) {
150+
// Log the actual type of change
151+
const changeType = isProblemChange ? 'New Problem' :
152+
isViewChange ? 'View Changed' :
153+
isActualRefresh ? 'Page Refresh' :
154+
'Page Load';
155+
console.log(`State change detected - ${changeType}`);
136156

137157
// Update last state
138158
lastState.problemPath = problemPath;
139159
lastState.view = currentView;
140160
lastState.lastPathname = pathname;
161+
lastState.lastUrl = url;
141162
lastState.lastUpdateTime = Date.now();
163+
lastState.lastTabId = tabId;
142164

143165
// Reset flags only on problem change or actual refresh
144166
if (isProblemChange || isActualRefresh) {
@@ -155,14 +177,22 @@ chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
155177
let descriptionTabUpdated = result.descriptionTabUpdated || false;
156178
let solutionsTabUpdated = result.solutionsTabUpdated || false;
157179

158-
// Always update description tab when in problem view
159-
if (currentView === 'problem') {
180+
// Only update description tab when needed
181+
if ((currentView === 'problem' || currentView === 'description') &&
182+
(!descriptionTabUpdated || isProblemChange || isActualRefresh) &&
183+
!isDescriptionEditorSwitch) {
160184
chrome.storage.local.set({ 'descriptionTabUpdated': true });
161-
chrome.tabs.sendMessage(tabId, { action: 'updateDescription', title: tab.title || 'title' });
185+
chrome.tabs.sendMessage(tabId, {
186+
action: 'updateDescription',
187+
title: tab.title || 'title',
188+
isRefresh: isActualRefresh,
189+
isProblemChange: isProblemChange,
190+
isViewChange: isViewChange
191+
});
162192
}
163193

164194
// Always update solutions tab when in solutions view
165-
if (currentView === 'solutions') {
195+
if (currentView === 'solutions'&&!solutionsTabUpdated) {
166196
chrome.storage.local.set({ 'solutionsTabUpdated': true });
167197
chrome.tabs.sendMessage(tabId, { action: 'updateSolutions', title: tab.title || 'title' });
168198
}

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

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
// shows the examples if the user has enabled it in the settings
22
function showExamples() {
3+
// Check if we're on the description tab before proceeding
4+
const isDescriptionPage = !window.location.href.includes('/solutions');
5+
if (!isDescriptionPage) {
6+
return;
7+
}
8+
39
chrome.storage.local.get(['showExamples'], (result) => {
410
const showExamples = result.showExamples;
511
const examples = document.querySelectorAll('div.flex.h-full.w-full')[0];
@@ -79,6 +85,12 @@ function observeThemeChanges() {
7985

8086
// show the leetcode difficulty if the user has enabled it in the settings
8187
function showDifficulty() {
88+
// Check if we're on the description tab before proceeding
89+
const isDescriptionPage = !window.location.href.includes('/solutions');
90+
if (!isDescriptionPage) {
91+
return;
92+
}
93+
8294
chrome.storage.local.get(['showDifficulty'], (result) => {
8395
const showDifficulty = result.showDifficulty;
8496
const difficultyContainer = document.querySelectorAll('div.relative.inline-flex')[0] as HTMLDivElement;
@@ -95,6 +107,12 @@ function showDifficulty() {
95107

96108
// show the leetcode problem rating if the user has enabled it in the settings
97109
function showRating(problemTitle: string) {
110+
// Check if we're on the description tab before proceeding
111+
const isDescriptionPage = !window.location.href.includes('/solutions');
112+
if (!isDescriptionPage) {
113+
return;
114+
}
115+
98116
chrome.storage.local.get(['showRating'], (result) => {
99117
const showRating = result.showRating;
100118
if (!showRating) {
@@ -148,6 +166,12 @@ function showCompanyTags(problemTitle: string) {
148166
return;
149167
}
150168

169+
// Check if we're on the description tab before proceeding
170+
const isDescriptionPage = !window.location.href.includes('/solutions');
171+
if (!isDescriptionPage) {
172+
return;
173+
}
174+
151175
// Try to find the description element with retries
152176
const maxRetries = 10;
153177
const baseDelay = 300;
@@ -349,7 +373,12 @@ function setupDescriptionThemeListener() {
349373

350374
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
351375
if (request.action === 'updateDescription') {
352-
// Detect theme on first load of a problem page
376+
// Only detect theme on first load, problem change, or refresh
377+
if (!request.isProblemChange && !request.isRefresh) {
378+
console.log('Skipping theme detection for internal navigation');
379+
return;
380+
}
381+
353382
console.log('Updating description tab...');
354383
detectAndSyncTheme();
355384
showExamples();

0 commit comments

Comments
(0)

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