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 c3be99e

Browse files
committed
more strict page refresh detection
1 parent 130dd7e commit c3be99e

File tree

2 files changed

+60
-25
lines changed

2 files changed

+60
-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: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,12 @@ function setupDescriptionThemeListener() {
373373

374374
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => {
375375
if (request.action === 'updateDescription') {
376-
// 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+
377382
console.log('Updating description tab...');
378383
detectAndSyncTheme();
379384
showExamples();

0 commit comments

Comments
(0)

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